[Tutor] Help Please
Mark Lawrence
breamoreboy at gmail.com
Wed Feb 20 20:23:29 EST 2019
On 20/02/2019 14:30, Mario Ontiveros wrote:
> Hello,
> I am new to python and have been stuck on this for a while. What I am trying to do is to remove rows with void, disconnected, and error on lines. The code I have does that, the only problem is that it removes my header because void is in header. I need to keep header.
>
> Any help will be greatly appreciated.
>
> with open("PSS.csv","r+") as f:
> new_f = f.readlines()
> f.seek(0)
> for line in new_f:
> if "Void" not in line:
> if "Disconnected" not in line:
> if "Error" not in line:
> f.write(line)
> f.truncate()
>
>
>
> Mario Ontiveros
>
Something like (completely from memory so untested) :-
with open("PSS.csv","r") as inf:
lines = inf.readlines()
with open("PSS.csv","w") as outf:
fiter = iter(lines)
line = next(fiter)
outf.write(line)
for line in fiter:
if "Void" not in line and "Disconnected" not in line and "Error"
not in line: # probably a simpler way of writing this but I'm knackered :-)
outf.write(line)
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
More information about the Tutor
mailing list