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)
4
  • This is a really bad way to code - your variable names should not be created dynamically Commented Feb 2, 2020 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 Commented Feb 2, 2020 at 20:19
  • Does this answer your question? How do I create a variable number of variables? Commented Feb 2, 2020 at 20:50
  • Do yu really want to use unpack? Commented Feb 2, 2020 at 21:05

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.