我在python 3.6.4中有这个简单的程序,当我使用excel 2010打开文件test.csv时,在csv文件中写入数据我发现结果写得像:
这是该计划:
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])
答案 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"构造函数中的参数