如何使用python将数据写入CSV文件?
我正在试图把下面的数据写入一个csv文件。数据是雇用的细节
名1 – surname1-PLACE1
名2-surname2-place2
NAME3-surname3-place3
NAME4-surname4-place4
我希望输出在一个在另一个下面的CSV文件在不同的行上。
我写了下面的代码
阅读器= csv.reader(文件)
op = open(path+“op.CSV”,“wb”)
String = list [0] +“ – ”+ list [1] +“ – ”+ list [2] +“ – ”+ list [4]
op.writer(String)
请帮忙。
提前致谢。
-KD
如果我已经很好地理解了你的问题,你正在寻找这个:
>>> import csv >>> employees = [ ... 'name1-surname1-place1', ... 'name2-surname2-place2', ... 'name3-surname3-place3', ... 'name4-surname4-place4', ... ] >>> with open('out.csv', 'w') as out: ... writer = csv.writer(out) ... for e in employees: ... writer.writerow(e.split("-")) ... >>> >>> host:~$ head out.csv name1,surname1,place1 name2,surname2,place2 name3,surname3,place3 name4,surname4,place4