CSV file written with Python has blank lines between each row

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

提交回复
热议问题