Hi I have two text files that each contain different information about certain structures.The structures are identified in both files by an id number. What I would like to do is read in the first file and skip lines of data that do not meet a condition. Then have lines in the second file that do not have the same id number skipped and the others processed. I tried to do it using nested for loops, and I also tried it as two separate function, but neither attempt worked. I am now trying to get it done using one loop as below, but get this error
UnboundLocalError: local variable 'linel' referenced before assignment
Here is the code. I
F = 'file1.txt'
Fl = 'file2.txt'
X = [] # Creats Data Lists
M = []
Id1 = []
Id2 = []
LC = 10
N = 11
fl = open(Fl)
fl.readline()
nlinesl = islice(fl,N)
f = open(F) #Opens file
f.readline() # Strips Header
nlines = islice(f, N) #slices file to only read N lines
for line in nlines and linel in nlinesl:
if line !='':
linel = linel.strip()
linel = linel.replace('\t','')
columnsl = linel.split()
lum = float(columnsl[1])
if lum != LC:
continue
id1 = int(columnsl[0])
Id1.append(id1)
if line !='':
line = line.strip()
line = line.replace('\t','')
columns = line.split()
id2 = int(columns[1])
Id2.append(id2)
if Id != Id2:
continue
x = columns[2] # assigns variable to columns
X.append(x)
print(X)
here is an example of what I would like to happen
Two files
file1= 1 1 1 1 file2 = 1 1 1 1
2 5 1 1 1 2 1 1
2 3 4 4 1 1 1 1
Lc = 5
Xa = 1
So only the second line of file1 will survive, which means that only the second line in file2 will be processed because they have the same id. In my files the ids are
id = columns[0] for file1
and
id = columns[1] for file2
Thanks in advance
I don’t think this is legal syntax:
Try this instead:
Also, these are tricky/easy to get wrong variable names 🙂
E.g.,
generates output as expected