0

If I were to read 10 input files and store them in variable names file1, file2, file3 etc that are dynamically created inside a for loop using string concatenation, how would I store the values in them? For example,

'file'+'1'=pandas.read_csv(...)

doesn't work. What are the alternatives to create variables dynamically to store values that are available dynamically?

Vysh
  • 718
  • 1
  • 7
  • 20
  • 5
    Just use a [`dict`](https://docs.python.org/3/library/stdtypes.html#dict) and store your file 'names' as keys and your CSV readers as values. – zwer May 17 '18 at 23:52

1 Answers1

4

One option is to use a dict structure:

datasets = {}
for i in range(10):
    key = "file{}".format(i)
    datasets[key] = pd.read_csv(...)
andrew_reece
  • 20,390
  • 3
  • 33
  • 58