我有以下列表,我想将它们存储在csv
示例中。你能帮我吗?我看过相关的post,但获得的csv
并不是我想要的。我现在已经过了输出,因为现在已经完成了这篇文章。
list1 = ['str1', 'str2','str3']
list2 = ['str1']
list3 = ['str1', 'str2']
lists.append(list1)
lists.append(list2)
lists.append(list3)
for l in lists:
'''save each list in a csv row where each element of the
list will be stored in separete cell.'''
CSV示例:
cell1 cell2 cell3
row1 str1 str2 str3
row2 str1
row3 str1 str2
CSV不受欢迎(将所有字符串放在第一个单元格中):
cell1 cell2 cell3
row1 str1 str2 str3
row2 str1
row3 str1 str2
答案 0 :(得分:1)
使用csv库:
import csv
with open('your_file.csv', 'wb') as csv_file:
csv_writer = csv.writer(csv_file, delimiter=";")
for l in lists:
csv_writer.writerow(l)
答案 1 :(得分:1)
或者就是这样:
file = open("newfile.csv", "w")
sep = ";" # your favorite separator
for l in list :
for e in l :
file.write (e + sep)
file.write ("\n")