CSV file written with Python has blank lines between each row

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

提交回复
热议问题