【发布时间】:2018-10-26 16:14:27
【问题描述】:
我有两个列表如下。
a=[1,3,5,6,7,12]
b=[23,45,67,67]
我需要将列表放在 csv 文件中的单独列中,如下所示:
Item,Quantity
[1,3,5,6,15],[23,45,67,67]
我尝试使用下面的代码 sn-ps,但没有得到我想要的结果。
with open('sample_dataset.csv', 'w', encoding = 'utf-16', newline='') as outfile:
rowlists = zip(a, b)
writer = csv.writer(outfile)
for row in rowlists:
writer.writerow(row)
结果是:
1,23
3,45
5,67
6,67
使用熊猫
d=[a,b]
my_df = pd.DataFrame(d)
my_df.to_csv('sample_dataset.csv', index=False, header=False)
结果在不同的行中:
1,3,5,6,7.0,12.0
23,45,67,67,,
感谢您的帮助和意见。谢谢
【问题讨论】:
-
您的输出示例不是有效的 csv。