使用python一个接一个地写两个列表到csv文件

时间:2018-07-12 09:39:26

标签: python python-2.7 list csv

我有两个列表,一个包含Date,另一个包含Rf

Date = ['July 12', 'July 13', 'July 14', 'July 15', 'July 16']
rf = ['12', '16', '18', '10', '4']

out = open('out.csv','ab')
writer = csv.writer(out)
writer.writerow(Date)
writer.writerow(rf)

但是csv中的输出并不像

Jul 12  Jul 13  Jul 14  Jul 15  Jul 16  
12      16      18      10      4

1 个答案:

答案 0 :(得分:3)

在Python 2.x中,可以将io.open与参数mode='w'newline=''一起使用。您还应该使用with语句:

Date = ['July 12', 'July 13', 'July 14', 'July 15', 'July 16']
rf = ['12', '16', '18', '10', '4']

import csv, io

with io.open('out.csv', 'wb', newline='') as out:
    writer = csv.writer(out)
    writer.writerow(Date)
    writer.writerow(rf)