Python3:以增量方式将多个词典写入CSV文件
我检查了主题:但我认为它还没有回答我的问题 我有几本字典,比如:Python3:以增量方式将多个词典写入CSV文件,python,python-3.x,csv,dictionary,Python,Python 3.x,Csv,Dictionary,我检查了主题:但我认为它还没有回答我的问题 我有几本字典,比如: {day:1, temperature: 30} {day:2, temperature: 40} 等等 词典不是立即准备好的,而是通过调度程序下载的 我想以以下方式写入文件: day temperature 1 30 2 40 并在新词典出现时继续附加到文件中 我如何使用Python 3实现这一点 非常感谢,使用csv模块和一系列字典L: import csv L = [{'day': 1, 'temperature':
{day:1, temperature: 30}
{day:2, temperature: 40}
等等
词典不是立即准备好的,而是通过调度程序下载的
我想以以下方式写入文件:
day temperature
1 30
2 40
并在新词典出现时继续附加到文件中
我如何使用Python 3实现这一点
非常感谢,使用
csv
模块和一系列字典L
:
import csv
L = [{'day': 1, 'temperature': 30},
{'day': 2, 'temperature': 40}]
with open(r'c:\temp\out.csv', 'w', newline='') as f:
wr = csv.writer(f)
wr.writerow(['day', 'temperature'])
for item in L:
wr.writerow([item['day'], item['temperature']])
结果:
day,temperature
1,30
2,40
使用
csv
模块和一组词典L
:
import csv
L = [{'day': 1, 'temperature': 30},
{'day': 2, 'temperature': 40}]
with open(r'c:\temp\out.csv', 'w', newline='') as f:
wr = csv.writer(f)
wr.writerow(['day', 'temperature'])
for item in L:
wr.writerow([item['day'], item['temperature']])
结果:
day,temperature
1,30
2,40