0

I have multiple text files, each containing several columns. I need to read each file into an array in python, called RDF. The point is that I used to read one file into one array as following:

RDF_1 = numpy.loadtxt("filename_1.txt", skiprows=205, usecols=(1,), unpak=True)

How to create a loop in python such that it reads more than one file into their corresponding arrays like this:

for i in range(100):
    RDF_i =  numpy.loadtxt("filename_"+str(i)+".txt", skiprows=205, usecols=(1,), unpak=True)
martineau
  • 119,623
  • 25
  • 170
  • 301
  • This is a really bad way to code - your variable names should not be created dynamically – Alan Kavanagh Feb 02 '20 at 20:17
  • Can you provide a higher level context to what you are trying to achieve? I don't think you need to store the results of `numpy.loadtxt` inside a variable each time -- you are risking reading a lot of information into memory – Alan Kavanagh Feb 02 '20 at 20:19
  • Does this answer your question? [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) – alkasm Feb 02 '20 at 20:50
  • Do yu really want to use `unpack`? – hpaulj Feb 02 '20 at 21:05

2 Answers2

1

You can use dictionaries as a proper way:

files_mapping = dict()
for i in range(100):
    files_mapping[f'RDF_{i}'] = numpy.loadtxt(f"filename_{i}.txt", skiprows=205, usecols=(1,), unpak=True)

But if for some unknown reasons you really need to dynamically create variables then you can use exec:

for i in range(100):
    exac(f'RDF_{i} = numpy.loadtxt(f"filename_{i}.txt", skiprows=205, usecols=(1,), unpak=True)'

And another possible way is using locals:

for i in range(100):
    locals()[f'RDF_{i}'] = numpy.loadtxt(f"filename_{i}.txt", skiprows=205, usecols=(1,), unpak=True)

You need to avoid using two last options in real code because it's a direct way to spawning hard-to-find bugs.

Charnel
  • 4,222
  • 2
  • 16
  • 28
0

I found a way to do it. I use two dimensional arrys after importing numpy library.

However, I had to zero the arrays before filling them out with data because python had already filled them out with random values.