根据其文档,csv.writer默认应使用'\ r \ n'作为换行符。
import csv
with open("test.csv", "w") as f:
writer = csv.writer(f)
rows = [(0,1,2,3,4),
(-0,-1,-2,-3,-4),
("a","b","c","d","e"),
("A","B","C","D","E")]
print writer.dialect.lineterminator.replace("\r", "\\r").replace("\n", "\\n")
writer.writerows(rows)
print writer.dialect.lineterminator.replace("\r", "\\r").replace("\n", "\\n")
此打印
\r\n
\r\n
如预期的那样。 但是,创建的csv文件使用换行符'\ r \ r \ n'
0,1,2,3,4
0,-1,-2,-3,-4
a,b,c,d,e
A,B,C,D,E
这是错误还是在我使用csv.writer时出现问题?
Python版本:
ActivePython 2.6.2.2(ActiveState 基于Python 2.6.2的Software Inc.) (r262:71600,2009年4月21日,15:05:37) [Win32上的MSC v.1500 32位(Intel)]
在Windows Vista上