Python - reading data from one file and selectively writing to a new file -
thanks in advance help.
i'm new @ python, , trying convert file 1 format another.
here code have:
fs = open('sample_data.txt','r') fnew = open('sample_output.txt','w') fs f: while true: line = f.readline() if line , line[0]=='#': print(line) fnew.write(line + '\n') else: data=line.split() fnew.write(data[0]) if not line: break print('end of program') fs.close fnew.close
the basic format of file contains commented headers @ top followed lines of data.
the issue i'm having fnew.write(data[0]) line. following error:
indexerror: list index out of range
the line split breaks 8 columns of data, of want remove first two. so, ultimately, want rewrite entire file minus first 2 columns. there few more complicated reformatting need do, i'm hoping if can understand error in step, may figure out how rest.
-------------- update
abarnet, you're right. it's newline causes error. however, i'm having issue when trying add check, said. when execute code below freezes on me. if remove "if data:" check, runs gives me same "index out of range" error.
i tried running below "if data:" check removed, , sample data file contains no newlines, , freezes on me.
can shed light on may causing this?
fs = open('sample_data.txt','r') fnew = open('sample_output.txt','w') fs f: while true: line = f.readline() line in f: if line[0]=='#': print(line) fnew.write(line + '\n') else: data=line.split() if data: print(data[0]) fnew.write(data[0] + '\n') print('end of program') fs.close fnew.close
-------------- update 2
this code below works. abarnet clarifying infinite loop issue. last issue i'm having first line of data, whether it's newline or header line ignored , not print in output.
with open('sample_data.txt','r') f, open('sample_output.txt','w') fnew: line = f.readline() line in f: if line[0]=='#': print(line) fnew.write(line + '\n') else: data=line.split() if data: print(data[0]) fnew.write(data[0] + '\n') print('end of program') fnew.close()
first, happens if line
empty?
you if not line: break
. before there, you'll first else:
(because it's not true line , line[0]=='#'
). so, data = line.split()
give data = []
. , data[0]
raise indexerror
.
just move if not line: break
test first:
while true: line = f.readline() if not line: break elif line[0]=='#': print(line) fnew.write(line + '\n') else: data=line.split() fnew.write(data[0])
that being said, there's easier way write in first place. looping on file give each line, 1 one, while
loop around readline
, except when gets eof, loop ends automatically without needing test or break
.
for line in f: if line[0]=='#': print(line) fnew.write(line + '\n') else: data=line.split() fnew.write(data[0])
but happens if line isn't empty, if it's blank, or pure whitespace? example, happens when call split()
on ' \n'
? again, empty list. so, if that's possible, you'll have same problem again—and, of course, don't want break
in case. i'm not sure want do, let's want skip on blank link. replace else
block this:
data=line.split() if data: fnew.write(data[0])
as side note, it's pretty weird fnew.write(line + '\n')
in first case, line ends in \n
you're adding newline, fnew.write(data[0])
in other case, data[0]
doesn't end in newline, you're merge run of first columns 1 giant word next comment tacked onto end…
the problem new code that, instead of replacing while true:
loop around readline()
for line in f:
loop, you've got both.
so, first time through while
loop, reads first line, reads every line in file, finishes. then, second time through while
loop, reads nothing left on @ end, reads 0 lines left over, finishes. , keeps going forever, reading last 0 lines on , on until end of time, because never break
out of while true:
.
you have few other problems in updated code.
fs.close
references method, without calling it. need parentheses call,fs.close()
.- but don't want
fs.close()
anyway; whole point ofwith
statement automatically closes file. - you want use
with
statementfnew
well.
so:
with open('sample_data.txt','r') f, open('sample_output.txt','w') fnew: line in f: if line[0]=='#': print(line) fnew.write(line + '\n') else: data=line.split() if data: print(data[0]) fnew.write(data[0] + '\n') print('end of program')
Comments
Post a Comment