CSV file written with Python has blank lines between each row

后端 未结 9 1561
青春惊慌失措
青春惊慌失措 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:36

    When using Python 3 the empty lines can be avoid by using the codecs module. As stated in the documentation, files are opened in binary mode so no change of the newline kwarg is necessary. I was running into the same issue recently and that worked for me:

    with codecs.open( csv_file,  mode='w', encoding='utf-8') as out_csv:
         csv_out_file = csv.DictWriter(out_csv)
    
    0 讨论(0)
  • 2020-11-21 10:37
    with open(destPath+'\\'+csvXML, 'a+') as csvFile:
        writer = csv.writer(csvFile, delimiter=';', lineterminator='\r')
        writer.writerows(xmlList)
    

    The "lineterminator='\r'" permit to pass to next row, without empty row between two.

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

    The simple answer is that csv files should always be opened in binary mode whether for input or output, as otherwise on Windows there are problems with the line ending. Specifically on output the csv module will write \r\n (the standard CSV row terminator) and then (in text mode) the runtime will replace the \n by \r\n (the Windows standard line terminator) giving a result of \r\r\n.

    Fiddling with the lineterminator is NOT the solution.

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

    Opening the file in binary mode "wb" will not work in Python 3+. Or rather, you'd have to convert your data to binary before writing it. That's just a hassle.

    Instead, you should keep it in text mode, but override the newline as empty. Like so:

    with open('/pythonwork/thefile_subset11.csv', 'w', newline='') as outfile:
    
    0 讨论(0)
  • 2020-11-21 10:43

    In Python 2, open outfile with mode 'wb' instead of 'w'. The csv.writer writes \r\n into the file directly. If you don't open the file in binary mode, it will write \r\r\n because on Windows text mode will translate each \n into \r\n.

    In Python 3 the required syntax changed (see documentation links below), so open outfile with the additional parameter newline='' (empty string) instead.

    Examples:

    # Python 2
    with open('/pythonwork/thefile_subset11.csv', 'wb') as outfile:
        writer = csv.writer(outfile)
    
    # Python 3
    with open('/pythonwork/thefile_subset11.csv', 'w', newline='') as outfile:
        writer = csv.writer(outfile)
    

    Documentation Links

    • https://docs.python.org/2/library/csv.html#csv.writer
    • https://docs.python.org/3/library/csv.html#csv.writer
    0 讨论(0)
  • 2020-11-21 10:45

    Borrowing from this answer, it seems like the cleanest solution is to use io.TextIOWrapper. I managed to solve this problem for myself as follows:

    from io import TextIOWrapper
    
    ...
    
    with open(filename, 'wb') as csvfile, TextIOWrapper(csvfile, encoding='utf-8', newline='') as wrapper:
        csvwriter = csv.writer(wrapper)
        for data_row in data:
            csvwriter.writerow(data_row)
    

    The above answer is not compatible with Python 2. To have compatibility, I suppose one would simply need to wrap all the writing logic in an if block:

    if sys.version_info < (3,):
        # Python 2 way of handling CSVs
    else:
        # The above logic
    
    0 讨论(0)
提交回复
热议问题