[英]how to print a specific part of a json file in python?
im new to python and i would like to know how a can print the specific data between the curly brackets in a json file.我是 python 的新手,我想知道如何在 json 文件中的大括号之间打印特定数据。 There are a lot of curly brackets, but i want the data between one pair of brackets.有很多大括号,但我想要一对括号之间的数据。 Here is a part of a the json:这是 json 的一部分:
"actual": {
"$id": "3",
"actualradarurl": "https://api.buienradar.nl/image/1.0/RadarMapNL?w=500&h=512",
"sunrise": "2019-10-21T08:14:00",
"sunset": "2019-10-21T18:34:00",
"stationmeasurements": [
{
"$id": "4",
"stationid": 6391,
"stationname": "Meetstation Arcen",
"lat": 51.5,
"lon": 6.2,
"regio": "Venlo",
"timestamp": "2019-10-21T14:30:00",
"weatherdescription": "Zwaar bewolkt",
"iconurl": "https://www.buienradar.nl/resources/images/icons/weather/30x30/c.png",
"graphUrl": "https://www.buienradar.nl/nederland/weerbericht/weergrafieken/c",
"winddirection": "ZW",
"temperature": 14.4,
"groundtemperature": 14.4,
"feeltemperature": 13.5,
"windgusts": 7.6,
"windspeed": 3.8,
"windspeedBft": 3,
"humidity": 72.0,
"precipitation": 0.0,
"sunpower": 100.0,
"rainFallLast24Hour": 3.2,
"rainFallLastHour": 0.0,
"winddirectiondegrees": 214
},
{
"$id": "5",
"stationid": 6275,
"stationname": "Meetstation Arnhem",
"lat": 52.07,
"lon": 5.88,
"regio": "Arnhem",
"timestamp": "2019-10-21T14:30:00",
"weatherdescription": "Zwaar bewolkt",
"iconurl": "https://www.buienradar.nl/resources/images/icons/weather/30x30/c.png",
"graphUrl": "https://www.buienradar.nl/nederland/weerbericht/weergrafieken/c",
"winddirection": "ZZW",
"airpressure": 1016.2,
"temperature": 14.9,
"groundtemperature": 15.3,
"feeltemperature": 13.1,
"visibility": 47200.0,
"windgusts": 11.3,
"windspeed": 7.5,
"windspeedBft": 4,
"humidity": 74.0,
"precipitation": 0.0,
"sunpower": 303.0,
"rainFallLast24Hour": 1.9,
"rainFallLastHour": 0.0,
"winddirectiondegrees": 197
},
JSON files can be loaded as python dictionaries. JSON 文件可以作为 python 字典加载。 So basically, what you want is to load the JSON as a dict object and print a given field of said object所以基本上,你想要的是加载 JSON 作为字典 object 并打印所述 object 的给定字段
import json
x :str = "{ 'key1': 'value1', 'key2': { 'subkey1': 'subval'} }"
dict_object = json.loads(x)
print(x['key2']) # Prints the dict {subkey1: subval}
print(x['key2']['subkey1']) # prints subval
import json
with open('data.txt') as json_file:
data = json.load(json_file)
print(data['actual']['stationmeasurements'][0])
For first set of curlies (idx = 0) it prints:对于第一组卷曲(idx = 0),它打印:
{'$id': '4', 'stationid': 6391, 'stationname': 'Meetstation Arcen', 'lat': 51.5, 'lon': 6.2, 'regio': 'Venlo', 'timestamp': '2019-10-21T14:30:00', 'weatherdescription': 'Zwaar bewolkt', 'iconurl': 'https://www.buienradar.nl/resources/images/icons/weather/30x30/c.png', 'graphUrl': 'https://www.buienradar.nl/nederland/weerbericht/weergrafieken/c', 'winddirection': 'ZW', 'temperature': 14.4, 'groundtemperature': 14.4, 'feeltemperature': 13.5, 'windgusts': 7.6, 'windspeed': 3.8, 'windspeedBft': 3, 'humidity': 72.0, 'precipitation': 0.0, 'sunpower': 100.0, 'rainFallLast24Hour': 3.2, 'rainFallLastHour': 0.0, 'winddirectiondegrees': 214}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.