为什么在使用python 3.6.4在csv文件中写入时跳过一行?

时间:2018-04-03 21:24:58

标签: python csv writing

我在python 3.6.4中有这个简单的程序,当我使用excel 2010打开文件test.csv时,在csv文件中写入数据我发现结果写得像:

enter image description here

这是该计划:

import csv   
lol = [(1,2,3),(4,5,6),(7,8,9)]
item_length = len(lol[0])

with open('test.csv', 'w') as test_file:
  file_writer = csv.writer(test_file)
  for i in range(item_length):
    file_writer.writerow([x[i] for x in lol])

2 个答案:

答案 0 :(得分:0)

newline=''添加到open()参数以防止此

with open('test.csv', 'w', newline='') as test_file:
    file_writer = csv.writer(test_file)
    for i in range(item_length):
        file_writer.writerow([x[i] for x in lol])

答案 1 :(得分:0)

解决方案是指定" lineterminator"构造函数中的参数