Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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字典中的链接_Python_Json_Dictionary_Url - Fatal编程技术网

python字典中的链接

python字典中的链接,python,json,dictionary,url,Python,Json,Dictionary,Url,所以对于一个学校项目,我必须通过api读入一些数据。到目前为止,一切(有点)都正常,但当我试图在几个链接下面读入一些数据时,会出现关键错误。 我不介意公布整个名单,但主要是荷兰语 import json from urllib.request import urlopen with urlopen("http://api.buienradar.nl/data/public/2.0/jsonfeed ") as response: source = response.read() d

所以对于一个学校项目,我必须通过api读入一些数据。到目前为止,一切(有点)都正常,但当我试图在几个链接下面读入一些数据时,会出现关键错误。 我不介意公布整个名单,但主要是荷兰语


import json
from urllib.request import urlopen

with urlopen("http://api.buienradar.nl/data/public/2.0/jsonfeed ") as response:
    source = response.read()

data = json.loads(source)
#pirnt(json.dumps(data, indent =2))

for item in data['actual']['stationmeasurements']:
    del item['iconurl']
    del item['graphUrl']
    station = item['stationname']
    #regio = item['regio]
    stationid = item['winddirection']
    print(station, stationid)


with open('uitlezen_buienradar.json', 'w') as f:
    json.dump(data, f , indent=2)
这是列表的一部分:

"$id": "1",
  "buienradar": {
    "$id": "2",
    "copyright": "(C)opyright Buienradar / RTL. Alle rechten voorbehouden",
    "terms": "Deze feed mag vrij worden gebruikt onder voorwaarde van bronvermelding buienradar.nl inclusief een hyperlink naar https://www.buienradar.nl. Aan de feed kunnen door gebruikers of andere personen geen rechten worden ontleend."
  },
  "actual": {
    "$id": "3",
    "actualradarurl": "https://api.buienradar.nl/image/1.0/RadarMapNL?w=500&h=512",
    "sunrise": "2019-10-04T07:45:00",
    "sunset": "2019-10-04T19:11:00",
    "stationmeasurements": [
      {
        "$id": "4",
        "stationid": 6391,
        "stationname": "Meetstation Arcen",
        "lat": 51.5,
        "lon": 6.2,
        "regio": "Venlo",
        "timestamp": "2019-10-04T02:30:00",
        "weatherdescription": "Zwaar bewolkt",
        "iconurl": "https://www.buienradar.nl/resources/images/icons/weather/30x30/cc.png",
        "graphUrl": "https://www.buienradar.nl/nederland/weerbericht/weergrafieken/cc",
        "winddirection": "ZO",
        "temperature": 10.0,
        "groundtemperature": 9.9,
        "feeltemperature": 9.6,
        "windgusts": 2.4,
        "windspeed": 1.5,
        "windspeedBft": 1,
        "humidity": 93.0,
        "precipitation": 0.0,
        "sunpower": 0.0,
        "rainFallLast24Hour": 4.5,
        "rainFallLastHour": 0.0,
        "winddirectiondegrees": 143
在“iconurl”和“graphurl”之后,它只会输出错误。

使用dict.get(键),如果键不存在,则不会出现异常。项目中的数据不完整;)


请发布一些我们可以运行和复制的内容。请在其EntertyPlease post error中显示错误并进行回溯为什么要删除项目中的条目?你们对它们什么都不做?我试着重现错误,只在“风向”上得到错误。为了避免错误,我建议您始终要获取一个密钥(如果item:print(item[“key”])中的“key”),对不起,我对这个网站和一般编程都很陌生。是的,URL需要删除。这是我唯一不需要的两个。也许读一下吧对不起,我对dict.get(key)不熟悉。我应该在哪里补充呢?或者修改密码?哦,对不起,我没看到。我现在唯一的问题是,它为什么会这样做?我的意思是,url有一个值,对吗?请参阅TomMP的解释。然后重试:
test={1:2}test[2]
引发key.error,因为键是
None。test.get(2)
不会引发错误。所以你的代码被执行了。您可以使用循环中的print(项目)检查数据,以查看dicts的结构。有些人没有想要的钥匙。
import json
from urllib.request import urlopen

with urlopen("http://api.buienradar.nl/data/public/2.0/jsonfeed ") as response:
    source = response.read()

data = json.loads(source)
#pirnt(json.dumps(data, indent =2))

for item in data['actual']['stationmeasurements']:
    del item['iconurl']
    del item['graphUrl']
    station = item.get('stationname')
    #regio = item['regio]
    stationid = item.get('winddirection')
    print(station, stationid)


with open('uitlezen_buienradar.json', 'w') as f:
    json.dump(data, f , indent=2)