I parse a .txt like this:
def parse_file(src):
for line in src.readlines():
if re.search('SecId', line):
continue
else:
cols = line.split(',')
Time = cols[4]
output_file.write('{}\n'.format(
Time))
I think cols are lists that I could use index. Although it succeeds in printing out correct result as I want, there exists an out of range error:
File “./tdseq.py”, line 37, in parse_file
Time = cols[4]
IndexError: list index out of range
make: * [all] Error 1
Data I use:
I10.FE,--,xx,xxxx,13450,tt,tt,tt,33,22,22:33:44
Without seeing the data, it’s hard to say.
Possible causes are that you are assuming 1-based indexing, when a line like:
foo,bar,baz,quxwould be indexed as positions 0,1,2,3 in the list.
By the way, I highly recommend you to parse your file using the csv module.