我如何从 python 中的不同输入打印到 CSV 文件

how do i print to a a CSV file from different inputs in python

如何将其更改为打印到 CSV 文件而不是文本文档。

name = billy
score = 69

f.write('Name: %s || Score: %i\n' % (name, score))

如果字典中有数据:

>>> player = {'name': 'Pete',
              'score': -10}

你可以照现在的样子写:

>>> f.write('Name: {name} || Score: {score}\n'.format(player))

如果你使用csv.DictWriter:

>>> writer = csv.DictWriter(f, fieldnames=player.keys())
>>> writer.writeheader()
>>> writer.writerow(player)