Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.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 Can';json文件中的t访问密钥_Python_Json - Fatal编程技术网

Python Can';json文件中的t访问密钥

Python Can';json文件中的t访问密钥,python,json,Python,Json,我尝试使用python处理此JSON文件: 我想使用以下代码访问'alignes'键: import json with open('arrets-reseau-irigo.json') as data_file: data = json.load(data_file) for i in data: print("datasetid is {0}".format(i['datasetid'])) print("nom arret is {0}

我尝试使用python处理此JSON文件:

我想使用以下代码访问
'alignes'
键:

import json

with open('arrets-reseau-irigo.json') as data_file:
    data = json.load(data_file)

    for i in data:
        print("datasetid is {0}".format(i['datasetid']))
        print("nom arret is {0}".format(i['fields']['nom_arret']))
        print("coordonnées is {0}".format(i['fields']['geo_point_2d']))
        try :
            if format(i['fields']['lignes']) in data :
                print("Fields : is {0}".format(i['fields']['lignes']))
            else :
                print('nothing')
        except:
            print("EXCEPTION")
结果是:

datasetid是arrets reseau irigo
名字叫arret是GIRARD
coordonnées为[47.4909169756,-0.581150255698]
没有什么
数据集是arrets reseau irigo
诺姆阿雷特是德维尔电车酒店B/C
coordonnées为[47.4716862858,-0.546754596835]
例外情况

您是否有示例代码来解决我的问题,并且仅当
'ligne'
键存在时才显示值?

使用您提供的文件示例

[
  {
    "datasetid": "arrets-reseau-irigo",
    "fields": {
      "accessib": "O",
      "date_maj": "Décembre 2016",
      "geo_point_2d": [
        47.4682358304,
        -0.550894481011
      ],
      "lignes": "L_1,L_1D,L_1S,L_2,L_2D,L_2S,L_3,L_3D,L_3S,L_4,L_6,L_10",
      "nom_arret": "FOCH - SAINT AUBIN",
      "source": "KEOLIS Réseau IRIGO"
    },
    "geometry": {
      "coordinates": [
        -0.550894481011,
        47.4682358304
      ],
      "type": "Point"
    },
    "record_timestamp": "2017-01-12T17:05:52+01:00",
    "recordid": "65e54c3d5e87a803c3a2199fbde5596e5833be8f"
  },
 ]
更新:

import json

with open('file.json') as data_file:
    data = json.load(data_file)

for i in data:
    print("datasetid is {0}".format(i['datasetid']))
    print("nom arret is {0}".format(i['fields']['nom_arret']))
    print("coordonnées is {0}".format(i['fields']['geo_point_2d']))
    description = i['fields'].get('lignes', 'nothing')
    print(description)

使用您提供的文件示例

[
  {
    "datasetid": "arrets-reseau-irigo",
    "fields": {
      "accessib": "O",
      "date_maj": "Décembre 2016",
      "geo_point_2d": [
        47.4682358304,
        -0.550894481011
      ],
      "lignes": "L_1,L_1D,L_1S,L_2,L_2D,L_2S,L_3,L_3D,L_3S,L_4,L_6,L_10",
      "nom_arret": "FOCH - SAINT AUBIN",
      "source": "KEOLIS Réseau IRIGO"
    },
    "geometry": {
      "coordinates": [
        -0.550894481011,
        47.4682358304
      ],
      "type": "Point"
    },
    "record_timestamp": "2017-01-12T17:05:52+01:00",
    "recordid": "65e54c3d5e87a803c3a2199fbde5596e5833be8f"
  },
 ]
更新:

import json

with open('file.json') as data_file:
    data = json.load(data_file)

for i in data:
    print("datasetid is {0}".format(i['datasetid']))
    print("nom arret is {0}".format(i['fields']['nom_arret']))
    print("coordonnées is {0}".format(i['fields']['geo_point_2d']))
    description = i['fields'].get('lignes', 'nothing')
    print(description)

如果我正确理解了这一点,而不是你想做的尝试/除外

if 'lignes' in i['fields']:
    print("Fields : is {0}".format(i['fields']['lignes']))
else :
    print('nothing')      

如果我正确理解了这一点,而不是你想做的尝试/除外

if 'lignes' in i['fields']:
    print("Fields : is {0}".format(i['fields']['lignes']))
else :
    print('nothing')      

lingnes键不存在于您发布的示例JSON文件中,但是它与say“format”处于同一级别。像这样的

for count in data:
    if 'fields' in data[count]:
        print(data[count]['fields']['format'])

lingnes键不存在于您发布的示例JSON文件中,但是它与say“format”处于同一级别。像这样的

for count in data:
    if 'fields' in data[count]:
        print(data[count]['fields']['format'])

要检查dict中是否存在密钥,请执行
如果输入dict:
...
因此,在您的情况下,
如果i[“字段”中的“对齐”:
...


目前,您正在检查
i['fields']['lignes']
的值是否是
数据的键,这没有任何意义,并且可能在整行计算完成之前抛出异常,即在计算
i['fields'][/lignes']时可能会出错,因为您还没有检查
'alignes'
是否是
i['fields']
的键

要检查dict中是否存在密钥,请执行
如果输入dict:
...
因此,在您的情况下,
如果i[“字段”中的“对齐”:
...


目前,您正在检查
i['fields']['lignes']
的值是否是
数据的键,这没有任何意义,并且可能在整行计算完成之前抛出异常,即在计算
i['fields'][/lignes']时可能会出错,因为您还没有检查
'alignes'
是否是
i['fields']
的键

你能告诉我们你的json文件是什么样子吗?我在那个json文件中没有看到任何“对齐”我修改了链接,错了对不起^^你能告诉我们你的json文件是什么样子吗?我在那个json文件中没有看到任何“对齐”我修改了链接,错了对不起^^Hi@Taras,我的文件错了^^我修改了链接。你可以检查:)我想用这个代码在'alignes'键访问我已经更新了答案。这行得通^^^但是为什么我的代码不行呢?“获取”的规则是什么?嗨@Taras,我的文件错了^^我修改了链接。你可以检查:)我想用这个代码在'alignes'键访问我已经更新了答案。这行得通^^^但是为什么我的代码不行呢?“获取”的规则是什么?我尝试了你的代码,但它总是显示“无”。:我更新了答案以删除数据中的附加检查
if格式(I['fields']['lignes']):
。我尝试了你的代码,但它总是显示“无”。:我更新了答案以删除数据中的附加检查
if格式(I['fields']['lignes'])。