tomo
tomo

Reputation: 13

How to use a variable as a parameter of a module?

Note: I am completely new to python and programming in general.
I am essentially using modules and packages for analysis purposes. Is it possible to put a variable as a argument of a module?

Let say I have 3 files:

a.csv 
b.csv 
c.csv

I have for each file a scaling number that I have saved in a tuple named scale [1,2,3,].

I would like to use a module that takes the file and its scaling number as arguments. It look like this

import module as mo
mo.function -scale 1 -input a.csv > output.csv

Instead of writing this numerous of times, I have 96 files, I would like to use a loop to do that for me.

Thank you very much for your help!

-- I hope it is better now

Upvotes: 1

Views: 85

Answers (2)

Tom83B
Tom83B

Reputation: 2109

I'm not sure what you mean by using a variable as a parameter of a module, but I assume that scale and input are arguments of mo.function.

What I would do in such case is loop through the files and the associated scaling numbers

import module as mo

my_files = ['file1','file2','file3']
scaling_numbers = [5, 6, 7]

for file, s in zip(my_files, scaling_numbers):
    mo.function(inp=file, scale=s)

Edit:

If you have 96 files, perhaps it will be useful for you to use the module os:

import module as mo
import os

directory = 'path/to/folder/with/your/files/'

my_files = [directory + file for file in os.listdir(directory)]
scaling_numbers = [5, 6, 7]

for file, s in zip(my_files, scaling_numbers):
    mo.function(inp=file, scale=s)

If you also have a list output files where you want to save the data, just include it in the zip:

for file_in, file_out, s in zip(input_files, output_files, scaling_numbers):
    mo.function(inp=file_in, out=file_out, scale=s)

Upvotes: 1

Guilherme Féria
Guilherme Féria

Reputation: 130

Could you better explain your question?

Upvotes: 0

Related Questions