我在python中有一个列表,我需要将输出写入csv。列表是:
>>>final_name[0]
[['Serbs'], ['Joachim RA'], [''], ['Mr RA'], [''], ['Albanian'], ['Boris Tadic', 'Serbia']]
>>> final_name[1]
[['Philippine', 'Gloria Macapagal Arroyo', 'Mitag'], ['Anthony Golez'], ['Golez'], [''], ['Golez'], ['']]
我需要csv中的输出为:
1 Serbs,Joachim RA,Mr RA,Albanian,...
2 Philippine,Gloria Macapagal,...
我已经尝试了所有东西,通过转换为字符串等...但我无法完成它...
怎么可能这样做。谢谢
答案 0 :(得分:1)
您是否尝试过使用join
?
lst = ["apricot", "banana", "cherry"]
result = ",".join(lst)
答案 1 :(得分:1)
试试这个
with open('test.csv', 'wb') as csvfile:
for i,j in enumerate(final_name, 1):
to_write = [item for sublist in j for item in sublist if item]
spamwriter = csv.writer(csvfile, delimiter=',')
spamwriter.writerow([i]+to_write)
$ cat test.csv
1,Serbs,Joachim RA,Mr RA,Albanian,Boris Tadic,Serbia
2,Philippine,Gloria Macapagal Arroyo,Mitag,Anthony Golez,Golez,Golez