0

I have the following code:

dat11=np.genfromtxt('errors11.txt') 
dat12=np.genfromtxt('errors12.txt') 
dat13=np.genfromtxt('errors13.txt') 
dat22=np.genfromtxt('errors22.txt') 
dat23=np.genfromtxt('errors23.txt') 
dat33=np.genfromtxt('errors33.txt')

zip(dat11,dat12,dat13,dat22,dat23,dat33)
import csv
with open('Allerrors.txt', "w+") as output:
    writer = csv.writer(output, delimiter='\t')
    writer.writerows(zip(dat11,dat12,dat13,dat22,dat23,dat33))
quit

Where each of the 'errorsxy.txt' files consists in a column of numbers. With this program I created the 'Allerrors.txt' file, were all those columns are one next to the others. I need to do this same thing with a for cycle (or any other kind of loop) because I'll actually have much more files and I can't do it by hand. But I don't know how to write these various datxy with a cycle. I tried (for the first part of the code) with:

for x in range(1,Nbin+1):
    for y in range(1,Nbin+1):
        'dat'+str(x)+str(y)=np.genfromtxt('errors'+str(x)+str(y)+'.txt')

But of course I get the following error:

SyntaxError: can't assign to operator

I understand why I get this error, but I couldn't find any other way to write it. Also, I have no idea how to write the second part of the code. I'm using Python 2.7 Anyone can help me?

Giulio
  • 5
  • 2

1 Answers1

0

Instead of making separate variables for each data file, you could append each read-in file to a list, then zip and print the list after the for loop has run.

errorfiles = []
for x in range(1,Nbin+1):
    for y in range(1,Nbin+1):
        dat=np.genfromtxt('errors'+str(x)+str(y)+'.txt’)
        errorfiles.append(dat)
divibisan
  • 11,659
  • 11
  • 40
  • 58