[英]how do i print to a a CSV file from different inputs in python
How do I change this to print to a CSV file instead of a text document. 如何将其更改为打印为CSV文件而不是文本文档。
name = billy
score = 69
f.write('Name: %s || Score: %i\n' % (name, score))
If you have data in a dictionary: 如果字典中有数据:
>>> player = {'name': 'Pete',
'score': -10}
You can write it as you currently are: 您可以像现在这样写:
>>> f.write('Name: {name} || Score: {score}\n'.format(player))
If you use csv.DictWriter
: 如果您使用
csv.DictWriter
:
>>> writer = csv.DictWriter(f, fieldnames=player.keys())
>>> writer.writeheader()
>>> writer.writerow(player)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.