CSV file written with Python has blank lines between each row

后端 未结 9 1562
青春惊慌失措
青春惊慌失措 2020-11-21 10:04
import csv

with open(\'thefile.csv\', \'rb\') as f:
  data = list(csv.reader(f))
  import collections
  counter = collections.defaultdict(int)

  for row in data:
          


        
相关标签:
9条回答
  • 2020-11-21 10:47

    Note: It seems this is not the preferred solution because of how the extra line was being added on a Windows system. As stated in the python document:

    If csvfile is a file object, it must be opened with the ‘b’ flag on platforms where that makes a difference.

    Windows is one such platform where that makes a difference. While changing the line terminator as I described below may have fixed the problem, the problem could be avoided altogether by opening the file in binary mode. One might say this solution is more "elegent". "Fiddling" with the line terminator would have likely resulted in unportable code between systems in this case, where opening a file in binary mode on a unix system results in no effect. ie. it results in cross system compatible code.

    From Python Docs:

    On Windows, 'b' appended to the mode opens the file in binary mode, so there are also modes like 'rb', 'wb', and 'r+b'. Python on Windows makes a distinction between text and binary files; the end-of-line characters in text files are automatically altered slightly when data is read or written. This behind-the-scenes modification to file data is fine for ASCII text files, but it’ll corrupt binary data like that in JPEG or EXE files. Be very careful to use binary mode when reading and writing such files. On Unix, it doesn’t hurt to append a 'b' to the mode, so you can use it platform-independently for all binary files.

    Original:

    As part of optional paramaters for the csv.writer if you are getting extra blank lines you may have to change the lineterminator (info here). Example below adapated from the python page csv docs. Change it from '\n' to whatever it should be. As this is just a stab in the dark at the problem this may or may not work, but it's my best guess.

    >>> import csv
    >>> spamWriter = csv.writer(open('eggs.csv', 'w'), lineterminator='\n')
    >>> spamWriter.writerow(['Spam'] * 5 + ['Baked Beans'])
    >>> spamWriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
    
    0 讨论(0)
  • 2020-11-21 10:49

    I'm writing this answer w.r.t. to python 3, as I've initially got the same problem.

    I was supposed to get data from arduino using PySerial, and write them in a .csv file. Each reading in my case ended with '\r\n', so newline was always separating each line.

    In my case, newline='' option didn't work. Because it showed some error like :

    with open('op.csv', 'a',newline=' ') as csv_file:
    
    ValueError: illegal newline value: ''
    

    So it seemed that they don't accept omission of newline here.

    Seeing one of the answers here only, I mentioned line terminator in the writer object, like,

    writer = csv.writer(csv_file, delimiter=' ',lineterminator='\r')

    and that worked for me for skipping the extra newlines.

    0 讨论(0)
  • 2020-11-21 10:50

    Use the method defined below to write data to the CSV file.

    open('outputFile.csv', 'a',newline='')
    

    Just add an additional newline='' parameter inside the open method :

    def writePhoneSpecsToCSV():
        rowData=["field1", "field2"]
        with open('outputFile.csv', 'a',newline='') as csv_file:
            writer = csv.writer(csv_file)
            writer.writerow(rowData)
    

    This will write CSV rows without creating additional rows!

    0 讨论(0)
提交回复
热议问题