用python读取文件
好的,我有一个python命令来写入文件用python读取文件,python,weather,Python,Weather,好的,我有一个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/weatherfi
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
回溯(最近一次呼叫最后一次):
文件“C:/Users/Grant/Desktop/getweather.py”,第3行,在
pp=pprint.预印机(缩进=4)
NameError:未定义名称“pprint”
我假设它是JSON数据,所以您可以使用以下方法打印它:
或者,您可以使用以下方法进行操作:
您得到的数据类型称为JSON编码。解决方案取自。当插入
indent=4
参数时,我通常会使用制表符和空格,您应该只获得spacesnow getting trackback错误jason没有为第一个错误定义,对于第二个建议i get pprint没有定义您没有导入模块:-)prependimport pprint
(更新我的答案)立即获取错误(unicode错误)'UnicodeScape"编解码器无法解码位置2-3处的字节:截断\uxxxxx转义不成功使用缩进和空格扫描您请使用当前程序编辑并更新问题,该程序会产生该错误?对于您的问题,我尝试了许多不同的方法来尝试您提供的信息,但它们都失败了,出现了某种类型的错误INVECTILD SYNTAX UNCEPT您能用产生该错误的当前程序编辑并更新问题吗?要打印json文件,您可以在命令行上运行:python-mjson.tool weatherfile.txt
import json
with open ('/home/pi/weatherfile.txt','r') as f:
d = json.loads(f.read())
json.dumps(sort_keys=True, indent=4, separators=(',', ': '))
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)
>>> 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"
}
]
}
}
>>>