Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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
编写和阅读词典Python 3_Python_Json_Dictionary_Weather - Fatal编程技术网

编写和阅读词典Python 3

编写和阅读词典Python 3,python,json,dictionary,weather,Python,Json,Dictionary,Weather,我想将天气信息写入一个文件,并用另一个脚本读取它。目前我一直在写文件。 原始代码: #!/usr/bin/env python3 from pprint import pprint import pywapi import pprint pp = pprint.PrettyPrinter(indent=4) steyregg = pywapi.get_weather_from_weather_com('AUXX0022') pp.pprint(steyregg) 这会给我这样的输出: &

我想将天气信息写入一个文件,并用另一个脚本读取它。目前我一直在写文件。 原始代码:

#!/usr/bin/env python3
from pprint import pprint
import pywapi
import pprint
pp = pprint.PrettyPrinter(indent=4)

steyregg = pywapi.get_weather_from_weather_com('AUXX0022')


pp.pprint(steyregg)
这会给我这样的输出:

> {   'current_conditions': {   'barometer': {   'direction': u'falling
> rapidly',
>                                                'reading': u'1021.33'},
>                               'dewpoint': u'0',
>                               'feels_like': u'2',
>                               'humidity': u'67',
>                               'icon': u'32',
>                               'text': u'W'}},.......
所以我试过了

#!/usr/bin/env python3
from pprint import pprint
import pywapi
import pprint
pp = pprint.PrettyPrinter(indent=4)

steyregg = pywapi.get_weather_from_weather_com('AUXX0022')

with open('weather.txt', 'wt') as out:
    pp.pprint(steyregg, stream=out)
但这会导致错误:

pprint() got an unexpected keyword argument 'stream'
我做错了什么?一旦wheater.txt在另一个python脚本中工作,我如何读取它?或者有没有更优雅的方法来捕获这样的数据并在其他地方使用

提前感谢

类的
pprint
方法不接受
关键字参数。在此行中创建对象时给出流:

pp = pprint.PrettyPrinter(indent=4)
或者使用函数,该函数接受
关键字参数

这就是错误的原因。一个更基本的问题是:当问题的标题是“编写和读取xml Python 3”时,为什么要使用
pprint
模块?
pprint
模块不生成XML。有关使用python处理XML的一些想法,请参见

还要注意,
pywapi.get\u weather\u from\u weather\u com
返回一个python字典。该函数已将XML数据转换为字典,因此您不必读取任何XML。看看这个(如果你还没有)

您可以将字典另存为JSON文件

import json

with open('weather.txt', 'wt') as out:
    json.dump(steyregg, out, indent=4)

我不需要xml,我认为这可以做到这一点,我只想天气信息保存和在另一个脚本中可用。我只是想找个办法,好的。我将用谷歌搜索如何处理这本词典。看看jsonI在答案中添加了一个关于JSON的注释。