[Tutor] Help Please

Alex Kleider akleider at sonic.net
Wed Feb 20 23:20:28 EST 2019


On 2019-02-20 06: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

Since your file seems to be a csv file, can we assume your 'header' line 
is really a comma separated list of column names?

If so, then using the csv module and specifically csv.DictReader (+/- 
DictWriter) might make things easier for you.


More information about the Tutor mailing list