-2

I can load the files from each month between 201501 and 202008 via

# list all months 
def iterate_months(start_ym, end_ym):
    for ym in range(int(start_ym), int(end_ym) + 1):
        if ym % 100 > 12 or ym % 100 == 0:
            continue
        yield str(ym)

yyyymm = list(iterate_months('201501', '202008'))

# load the files from each month
for i in range(len(yyyymm)):
     Research_files[i] = sorted(glob.glob('Research_observations'+yyyymm[i]+'*.csv')) 

But the variable name Research_files[i] does not show the year and month directly. This is a problem when there are too many months.

Is there a way to name them as "Research_file_201501", "Research_file_201502"..."Research_file_202008" automatically when loaded?

martineau
  • 119,623
  • 25
  • 170
  • 301
Jeremy
  • 849
  • 6
  • 15

1 Answers1

2

yes, use exec to pass the variable name as string.

code_string = "Research_file_"+yyyymm[i]+" = sorted(glob.glob('Research_observations'+yyyymm[i]+'*.csv')) "
exec(code_string)

maede rayati
  • 756
  • 5
  • 10
  • It worked. There are other questions look similar, but I really did not know how to apply those to my case. Yours are very straightforward. Many thanks for this! – Jeremy Aug 17 '20 at 22:15