[英]Python Writing Multiple Unequal Lists to Excel file
I have mutiple lists 我有多个清单
ANNA=(1, 2, 3, 4)
MAE=(1,2)
CEL=(1,2,3,4,5,6,7)
I want to write them in an excel or csv file format which looks like this: 我想以excel或csv文件格式编写它们,如下所示:
When I used csv writer, i need to use writerow
with additonal '
so that the values will not be truncated in excel. 当我使用csv writer时,我需要将
writerow
与additonal '
一起使用,以使值不会在excel中被截断。 It's fine, but still the other column is not appearing. 很好,但是其他栏仍然没有出现。
I use python3.x so izip does not work I guess coz I've tried it. 我使用python3.x,所以izip无法正常工作,我想是因为我已经尝试过了。 I tried
pandas
and DataFrame
but the columns need to be of equal lengths and my data is big and i do not know how to place NaN
to my missing values. 我尝试了
pandas
和DataFrame
但是列的长度必须相等,并且我的数据很大,而且我不知道如何将NaN
为缺少的值。 Besides, I wonder if there is a way instead of replacing NaN
to equalize my columns it does not look good. 此外,我想知道是否有办法代替
NaN
来均衡我的列,但它看起来并不好。
Thanks in advance! 提前致谢!
I think this works for you: 我认为这对您有用:
import csv
from itertools import zip_longest
ANNA = (1, 2, 3, 4)
MAE = (1, 2)
CEL = (1, 2, 3, 4, 5, 6, 7)
with open('output.csv', 'w') as outcsv:
writer = csv.writer(outcsv)
writer.writerow(["ANNA", "MAE", "CEL"])
for row in zip_longest(ANNA, MAE, CEL):
writer.writerow(row)
Reference: 参考:
You can use zip_longest
in python 3: 您可以在python 3中使用
zip_longest
:
>>> x = zip_longest(ANNA, MAE, CEL)
>>> [k for k in x]
[(1, 1, 1), (2, 2, 2), (3, None, 3), (4, None, 4), (None, None, 5), (None, None, 6), (None, None, 7)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.