Coolcrab
Coolcrab

Reputation: 2723

Python: np.loadtxt, read multiple files

I have managed to get loadtxt to read in a single file, but now I want it to read in a bunch of files off a .list file I have. I tried throwing it in a for loop, but I can't seem to get it to work. Can anyone help please?

[row1, row2, row3] = np.loadtxt("data.fits",unpack=True,skiprows=1)

And I want something like

for i in range(0,len(array)):
   [row1, row2, row3] = np.loadtxt("list.list[i]",unpack=True,skiprows=1)
   DO THINGS

Upvotes: 2

Views: 5128

Answers (2)

Daniel
Daniel

Reputation: 19547

for i in range(len(array)):
   [row1, row2, row3] = np.loadtxt(list.list[i],unpack=True,skiprows=1)

Additionally:

filelist=['file1','file2']
for file in filelist:
    [row1, row2, row3] = np.loadtxt(file,unpack=True,skiprows=1)
    #Do Stuff

I believe the quotation marks is messing with you. Also you do not need the 0 in range.

If this doesnt work can you paste what list.list is and array?

Upvotes: 3

Jon Clements
Jon Clements

Reputation: 142136

import fileinput
data = fileinput.input(['file1.txt', 'file2.txt', 'file3.txt'])

Then use that...

Upvotes: 0

Related Questions