Skip to content Skip to sidebar Skip to footer

Removing Lines From A Csv With Python Also Adds An Extra Line

This code, borrowed from another place on stackoverflow, removes all of the places that the csv has 'None' written in it. However, it also adds an extra line to the csv. How can I

Solution 1:

If you want to replace all the None's:

with open(filename) as f:
     lines = f.read().replace("None","")
with open(filename,"w") as f1:
     f1.write(lines)

Using rstrip with fileinput should also work:

import fileinput
for line in fileinput.FileInput(fileinput,inplace=1):
        print  line.replace('None',"").rstrip() # remove newline character to avoid adding extra lines in the output

Solution 2:

The problem here has nothing to do with fileinput, or with the replace.

Lines read from a file always end in a newline.

print adds a newline, even if the thing you're printing already ends with a newline.

You can see this without even a file involved:

>>>a = 'abc'>>>print a
abc
>>>a = 'abc\n'>>>print a
abc

>>>

The solution is any of the following:

  • rstrip the newlines off the input: print line.rstrip('\n') (or do the strip earlier in your processing)
  • Use the "magic comma" to prevent print from adding the newline: print line,
  • Use Python 3-style print with from __future__ import print_function, so you can use the more flexible keyword arguments: print(line, end='')
  • Use sys.stdout.write instead of print.
  • Completely reorganize your code so you're no longer writing to stdout at all, but instead writing directly to a temporary file, or reading the whole file into memory and then writing it back out, etc.

Post a Comment for "Removing Lines From A Csv With Python Also Adds An Extra Line"