简体   繁体   English

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

[英]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.

相关问题 如何从 python 中的 CSV 文件打印特定字段? - How do I print a particular field from a CSV file in python? 如何在python中打印csv文件中的记录总数? - How do I print the total number of records from a csv file in python? 如何使用 Python 仅打印 csv 文件的前 10 行? - How do I print only the first 10 lines from a csv file using Python? 如何计算csv文件中的特定数据并在python中打印该数字? - How do I count specific data from a csv file and print that number in python? 如何在python中从CSV文件中打印选定的列 - How can I print selected columns from a CSV file in python 如何在csv文件中的行中打印特定字段,以及如何将输入内容写入csv文件? - How do I print specific fields from rows in a csv file and how to write input to a csv file? 如何从CSV文件获取输入并使用Python编写特定的输出? - How to take inputs from a CSV file and write a specific output with Python? Python:如何从.txt文件中打印unicode字符串 - Python: How do I print an unicode string from a .txt file 如何在python中以数字方式对csv文件中的数据进行排序 - how do i sort data from a csv file numerically in python 从 csv 文件打印时如何删除 [] 和 '' python 3 - How do I remove [] and '' when printing from a csv file python 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM