[英]Appending a thing in csv file in python in an appropriate way
我正在尝试按照我被询问的顺序将一些东西放入我的 csv 文件中。
output 应该是:
Name: 'Bill Biilys'
Address: 'helloworld 22'
Price: '22.99'
但我的 output 就像:
Bill Billyshelloworld 2222.99
我将如何修改我的代码以完成此操作?
with open('alldata.csv','a',) as Alldata:
Alldata.write(Name)
Alldata.write(Address)
Alldata.write(Date)
Alldata.write(str(Price))
Alldata.write(str(ID))
Alldata.write(str(TimesOrdered))
Alldata.write('\n')
使用 csv 模块尝试类似的操作。 'list_of_things' 将是 append 所需的项目。 将您的数据放入列表是这里的关键。 但是可以根据需要采取和修改解决方案以解决您的问题。
import csv
with open("output.csv", 'a', newline= '') as output:
wr = csv.writer(output, dialect='excel')
for element in list_of_things:
wr.writerow([element])
output.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.