2

好的,所以我在 python 中有一个命令来写入文件

 from urllib.request import urlretrieve
urlretrieve('http://www.myweather2.com/developer/forecast.ashx?uac=29bsTnYaZT&output=json&query=50323&temp_unit=f','weatherfile.txt')

然后是打开该文件的命令

with open ('/home/pi/weatherfile.txt','r') as f:
    print(f.read())

这是我得到的输出

{ "weather": { "curren_weather": [ {"humidity": "62", "pressure": "1007", "temp": "86", "temp_unit": "f", "weather_code": "1", "weather_text": "Partly cloudy",  "wind": [ {"dir": "S", "speed": "6", "wind_unit": "kph" } ] } ],  "forecast": [ {"date": "2013-06-26",  "day": [ {"weather_code": "0", "weather_text": "Sunny skies",  "wind": [ {"dir": "W", "dir_degree": "266", "speed": "11", "wind_unit": "kph" } ] } ], "day_max_temp": "93",  "night": [ {"weather_code": "0", "weather_text": "Clear skies",  "wind": [ {"dir": "NW", "dir_degree": "313", "speed": "22", "wind_unit": "kph" } ] } ], "night_min_temp": "68", "temp_unit": "f" }, {"date": "2013-06-27",  "day": [ {"weather_code": "1", "weather_text": "Partly cloudy skies",  "wind": [ {"dir": "NNE", "dir_degree": "27", "speed": "18", "wind_unit": "kph" } ] } ], "day_max_temp": "86",  "night": [ {"weather_code": "29", "weather_text": "Thundery outbreaks possible",  "wind": [ {"dir": "NNE", "dir_degree": "20", "speed": "18", "wind_unit": "kph" } ] } ], "night_min_temp": "65", "temp_unit": "f" } ] }}

我希望它输出更好一点有没有办法我可以做到这一点?

这是我尝试过的更新

froom urllib.request import urlretrieve
urlretrieve('http://api.wunderground.com/api/015098ae0bc15cce/conditions/q/IA/Urbandale.json','weatherfile.txt')
pp = pprint.PrettyPrinter(indent=4)
with open ('/home/pi/weatherfile.txt','r') as f:
    d = json.loads(f.read())
    pp.pprint(d)

我得到这个错误

Traceback (most recent call last):
File "C:/Users/Grant/Desktop/getweather.py", line 3, in <module>
pp = pprint.PrettyPrinter(indent=4)
NameError: name 'pprint' is not defined  
4

2 回答 2

2

我假设它是 JSON 数据,因此您可以使用json漂亮地打印它:

import json
with open ('/home/pi/weatherfile.txt','r') as f:
    d = json.loads(f.read())
    json.dumps(sort_keys=True, indent=4, separators=(',', ': '))

或者你可以使用pprint来做到这一点:

import json
import pprint
pp = pprint.PrettyPrinter(indent=4)
with open ('/home/pi/weatherfile.txt','r') as f:
    d = json.loads(f.read())
    pp.pprint(d)
于 2013-06-26T00:41:10.263 回答
1
>>> Data
{'weather': {'curren_weather': [{'temp_unit': 'f', 'temp': '86', 'weather_code': '1', 'humidity': '62', 'pressure': '1007', 'weather_text': 'Partly cloudy', 'wind': [{'wind_unit': 'kph', 'speed': '6', 'dir': 'S'}]}], 'forecast': [{'temp_unit': 'f', 'day_max_temp': '93', 'night_min_temp': '68', 'night': [{'weather_text': 'Clear skies', 'weather_code': '0', 'wind': [{'dir_degree': '313', 'speed': '22', 'wind_unit': 'kph', 'dir': 'NW'}]}], 'date': '2013-06-26', 'day': [{'weather_text': 'Sunny skies', 'weather_code': '0', 'wind': [{'dir_degree': '266', 'speed': '11', 'wind_unit': 'kph', 'dir': 'W'}]}]}, {'temp_unit': 'f', 'day_max_temp': '86', 'night_min_temp': '65', 'night': [{'weather_text': 'Thundery outbreaks possible', 'weather_code': '29', 'wind': [{'dir_degree': '20', 'speed': '18', 'wind_unit': 'kph', 'dir': 'NNE'}]}], 'date': '2013-06-27', 'day': [{'weather_text': 'Partly cloudy skies', 'weather_code': '1', 'wind': [{'dir_degree': '27', 'speed': '18', 'wind_unit': 'kph', 'dir': 'NNE'}]}]}]}}
>>> import json
>>> print json.dumps (Data,  sort_keys=True, indent=4, separators=(',', ': '))
{
    "weather": {
        "curren_weather": [
            {
                "humidity": "62",
                "pressure": "1007",
                "temp": "86",
                "temp_unit": "f",
                "weather_code": "1",
                "weather_text": "Partly cloudy",
                "wind": [
                    {
                        "dir": "S",
                        "speed": "6",
                        "wind_unit": "kph"
                    }
                ]
            }
        ],
        "forecast": [
            {
                "date": "2013-06-26",
                "day": [
                    {
                        "weather_code": "0",
                        "weather_text": "Sunny skies",
                        "wind": [
                            {
                                "dir": "W",
                                "dir_degree": "266",
                                "speed": "11",
                                "wind_unit": "kph"
                            }
                        ]
                    }
                ],
                "day_max_temp": "93",
                "night": [
                    {
                        "weather_code": "0",
                        "weather_text": "Clear skies",
                        "wind": [
                            {
                                "dir": "NW",
                                "dir_degree": "313",
                                "speed": "22",
                                "wind_unit": "kph"
                            }
                        ]
                    }
                ],
                "night_min_temp": "68",
                "temp_unit": "f"
            },
            {
                "date": "2013-06-27",
                "day": [
                    {
                        "weather_code": "1",
                        "weather_text": "Partly cloudy skies",
                        "wind": [
                            {
                                "dir": "NNE",
                                "dir_degree": "27",
                                "speed": "18",
                                "wind_unit": "kph"
                            }
                        ]
                    }
                ],
                "day_max_temp": "86",
                "night": [
                    {
                        "weather_code": "29",
                        "weather_text": "Thundery outbreaks possible",
                        "wind": [
                            {
                                "dir": "NNE",
                                "dir_degree": "20",
                                "speed": "18",
                                "wind_unit": "kph"
                            }
                        ]
                    }
                ],
                "night_min_temp": "65",
                "temp_unit": "f"
            }
        ]
    }
}
>>> 

您获得的数据类型称为 JSON 编码。取自http://docs.python.org/2/library/json.html的解决方案。

于 2013-06-26T00:42:19.753 回答