Jun-28-2021, 09:47 AM
Hey!
I have a text file that I want to sort out. I've coded this and tried dataframe, but that only prints the last line. The code I have now is this, producing the txt file:
I have a text file that I want to sort out. I've coded this and tried dataframe, but that only prints the last line. The code I have now is this, producing the txt file:
with open(output) as file, open(out, 'w') as file_out: for line in file: if '2101' in line and found: a = line.split() print(a[1], file=file_out) elif 'Lifetimes' in line and found: b = line.split() print(b[3], b[4], b[5], file=file_out) elif 'Std deviations' in line and found: # print(c[3:6]) c = line print(deviations(c), file=file_out) elif 'Intensities' in line and found: d = line.split() print(d[3], d[4], d[5], file=file_out) elif 'Time-zero' in line and found: e = line.split() print(e[4], file=file_out) else: found = True #This is what I tried so far with open(out) as a: cpt = 0 for line in a: cpt += 1 if cpt == 8: print(line) cpt = 0The 'out' file is like this:
Number Value1 Deviation Value2 Deviation Value3 Deviation Number Value1 Deviation ...So basically the file is now a list I want to sort so that Lifetimes are all in one column, Value1 in the next, then Deviation, Value2, its deviation so on; I want every 8th value in the same column, and I'm guessing this could somehow be done by creating a loop that prints, skips 7 values and prints the next so that the start number could be changed from 1-7. I need to save the results in another file, so perhaps it'd be easier to code the columns in the 'out' file already without creating so many files, but for now it's enough to get the data sorted properly, so even the simplest code to produce columns from the txt file works!