CSV file written with Python has blank lines between each row

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

提交回复
热议问题