Javier Bilbao
Javier Bilbao

Reputation: 11

How to read consecutive .plt files and store them

I have 1000 files that I want to read with python. Before, I had 74 and I just read them one by one, but now there are too much to do so.

data_1 = np.genfromtxt('test4-1.000001.plt', delimiter=' ', skip_header=3)
data_2 = np.genfromtxt('test4-1.000002.plt', delimiter=' ', skip_header=3)         
data_3 = np.genfromtxt('test4-1.000003.plt', delimiter=' ', skip_header=3)         
data_4 = np.genfromtxt('test4-1.000004.plt', delimiter=' ', skip_header=3)         
data_5 = np.genfromtxt('test4-1.000005.plt', delimiter=' ', skip_header=3)         
data_6 = np.genfromtxt('test4-1.000006.plt', delimiter=' ', skip_header=3)         
data_7 = np.genfromtxt('test4-1.000007.plt', delimiter=' ', skip_header=3) 
.
.
.
.
.
data_73 = np.genfromtxt('test4-1.000073.plt', delimiter=' ', skip_header=3)
data_74 = np.genfromtxt('test4-1.000074.plt', delimiter=' ', skip_header=3)

I have tried to use a loop for, but it doesn't work. I tried to make i be a string, but still didn't work

for i in range(1:1000):
     data_'i' = np.genfromtxt('test4-1.00000'i'.plt', delimiter=' ',skip_header=3)

Thank you very much for the help

Upvotes: 0

Views: 5057

Answers (2)

Loocid
Loocid

Reputation: 6441

You kinda have the right idea but you made a few syntax mistakes, and it looks like you need to learn about lists.

What you should do is make a list of all your data. Lets call it data.

We define an empty like like this: data = []

To add to a list we use the append command like this:

data.append(np.genfromtxt('test4-1.000001.plt', delimiter=' ',skip_header=3))

The last thing is you were trying to concatenate your strings wrong. In python you concatenate strings with a + so this line: 'test4-1.00000'i'.plt' should be 'test4-1.'+str(i).zfill(6)+'.plt'. Note I had to turn it into a string first and I also used the zfill function to pad it with zeros.

Putting all this together we have the code

data = []
for i in range(1:1000):
    data.append(np.genfromtxt('test4-1.'+str(i).zfill(6)+'.plt', delimiter=' ',skip_header=3))

This will leave you with a list of data that you can call from using the syntax data[23] to get the 24th data set (list indexing starts at 0).

Hope this helped!

Upvotes: 4

Rick Sullivan
Rick Sullivan

Reputation: 188

data = []
for i in range(1, 1000):
     data.append(np.genfromtxt('test4-1.%06d.plt' % i, delimiter=' ',skip_header=3))

Edit

Loocid's answer explains how each part of this works.

Upvotes: 1

Related Questions