How to read files with similar names but without writting all the names? python -
i have written code, wish in cleverer way, using loop write names instead.
my code is:
file_name_01 = os.path.join(input_folder_name,'subject101.dat') file_df_01 = pd.read_table(file_name_01, ' ', header=none) file_name_02 = os.path.join(input_folder_name,'subject102.dat') file_df_02 = pd.read_table(file_name_02, ' ', header=none) file_name_03 = os.path.join(input_folder_name,'subject103.dat') file_df_03 = pd.read_table(file_name_03, ' ', header=none) file_name_04 = os.path.join(input_folder_name,'subject104.dat')
but want this:
for(i=0, i<9, i++) file_name_0%i = os.path.join(input_folder_name,'subject10%i.dat') file_df_0%i = pd.read_table(file_name_0%i, ' ', header=none)
i looked answer, found solutions r, java , other languages.
i need in python, if me, happy.
instead of dynamically building variable names, use dict
files = dict() in range(9): file_name = os.path.join(input_folder_name, 'subject10{}'.format(i)) files[file_name] = pd.read_table(file_name , ' ', header=none)
Comments
Post a Comment