[英]How to write single elements of a list at the end of a csv line with Python?
下面的代碼將完整列表['x', 'y', 'z']
添加為 csv 的單個元素。 我想要得到的是'x'; 'y'; 'z';
'x'; 'y'; 'z';
在行尾。 如何?
import csv
toto = ['x', 'y', 'z']
with open('res.csv', 'w', newline='') as resfile:
reswriter = csv.writer(resfile, delimiter=';')
reswriter.writerow(['Number', 'A', 'B', 'X', 'Y', 'Z']) # header.
reswriter.writerow(['001', 'a', 'b', toto])
將星號*
添加到toto
:
import csv
toto = ['x', 'y', 'z']
with open('res.csv', 'w', newline='') as resfile:
reswriter = csv.writer(resfile, delimiter=';')
reswriter.writerow(['Number', 'A', 'B', 'X', 'Y', 'Z']) # header.
reswriter.writerow(['001', 'a', 'b', *toto]) # <--- note the *
寫res.csv
:
Number;A;B;X;Y;Z
001;a;b;x;y;z
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.