I am new to Python, and I have a problem in dealing with multiple data files. I want to read multiple data files into multiple arrays, for example, I want to read data in 1c.txt to array c1, data in 2c.txt into c2, etc. And I tried the following code:
import numpy as np
for i in range(1,15):
globals()['c%s' % i] = np.loadtxt(['%sc.txt' % i], usecols=(0,1,2))
But it prompted with IndexError: list index out of range
, and I changed usecols=(0,1,2)
to usecols=(0)
it still didn't work, so I think something else must be wrong.
Also I found I could not use the code as:
['c%s' % i]
to get the variable names as c1,c2, etc. And I have to add a globals()
before the ['c%s' % i]
, but I don't know why.
Waiting online. Many thanks!