首先文件名不能用python已有的或自己创建的模块名。然后:
import csv
csvFile=open("C:/Users/hasee/Desktop/wikispider/test.csv",'w',newline='')
try:
writer=csv.writer(csvFile)
writer.writerow(('number','number plus 2','number times 2'))
for i in range(10):
writer.writerow((i,i+2,i*2))
finally:
csvFile.close()
红色字体部分解决带空行的结果。具体原因可见stackoverflow。
In Python 3.X, the csv module still does its own line termination handling, but still needs to know an encoding for Unicode strings.(摘自stackoverflow)