Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python3:以增量方式将多个词典写入CSV文件_Python_Python 3.x_Csv_Dictionary - Fatal编程技术网

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