我需要添加city ='Delhi'和age = 25以及人名列表,并将其写入csv。
import csv
p1 = ['Peter', 'Sam', 'Mike']
city = ['Delhi']
Age = ['25']
def write_to_csv(name):
with open('person.csv', 'w') as csvfile:
writer = csv.writer(csvfile, delimiter='\n')
writer.writerow(name)
write_to_csv(p1)
当前O / P
彼得
山姆
迈克
必需的输出
彼得,德里,25
Sam,Delhi,25
迈克,德里,25
答案 0 :(得分:1)
只需遍历每个元素
def write_to_csv(name):
with open('person.csv', 'w') as csvfile:
writer = csv.writer(csvfile, delimiter=',')
for i in range(len(name)):
writer.writerow([name[i],city[0],Age[0]])
答案 1 :(得分:0)
添加\ n行终止符后,o / p看起来不错。谢谢
import csv
p1 = ['Peter', 'Sam', 'Mike']
city ='Delhi'
Age = '25'
def write_to_csv(name):
with open('person.csv', 'w') as csvfile:
writer = csv.writer(csvfile, delimiter=',',lineterminator='\n')
for i in range(len(name)):
writer.writerow([name[i], city, Age])
write_to_csv(p1)