Home > Mobile >  how to package all parameters as one?
how to package all parameters as one?

Time:12-09

I always import one function.py to do some special calculate,

one day I found this function.py have steps of read files, that means, every time I call this function, function.py will open several excel files.

file1 = pd.read_excel('para/first.xlsx',sheet_name='Sheet1')
file2 = pd.read_excel('para/second.xlsx',sheet_name='Sheet1')
...

I think it's a waste of time, can any method to pacakage all the excel files as one parameter, so I can read the files in main script, other than open it many times in function.py.

function.calculate(parameter_group)

to replace

function.calculate(file1,file2, file3...)

how to get "parameter_group"?

I'm thinking if make those files parameters to .pkl, maybe can read faster?

CodePudding user response:

You can loop through the names and put the DataFrames into a list or you could within the loop do whatever calculations you want and save the results in a list. For example:

pd_group = []
file_names = ['file1', 'file2', 'file3']

for name in file_names:
    pd_group.append(pd.read_excel(name,sheet_name='Sheet1'))

then access the DataFrames using pd_group[0], pd_group[1] and so on. Or else assign individual names as you want by using myname0 = pd_group[0] and so on.

CodePudding user response:

  1. define a class

  2. read excel in init

     class Demo:
    
         def __init__(self):
             self.excel_info = self.mock_read_excel()
             print("done")
    
         def mock_read_excel(self):
             # df = pd.read_excel('para/second.xlsx',sheet_name='Sheet1')
             df = "mock data"
             print("reading")
             return df
    
         def use_df_data_1(self):
             print(self.excel_info)
    
         def use_df_data_2(self):
             print(self.excel_info)
    
    
     if __name__ == '__main__':
         dm = Demo()
         dm.use_df_data_1()
         dm.use_df_data_2()
    

It can solve the problem of reading excel every time the function is called

  • Related