首页 文章

尝试从JSON列表中打印元素时出现键错误

提问于
浏览
1

我有这个从postcodes.io检索的邮政编码数据 - 我正在开发一些Python代码来将这些数据读入JSON结构 . 然后,我希望打印出此结构中的国家和地区项目 .

{                                                                                                                                                                                
  "status": 200,                                                                                                                                                                 
  "result": {                                                                                                                                                                    
    "eastings": 536500,                                                                                                                                                          
    "outcode": "SG8",                                                                                                                                                            
    "admin_county": "Hertfordshire",                                                                                                                                             
    "postcode": "SG8 7XU",                                                                                                                                                       
    "incode": "7XU",                                                                                                                                                             
    "codes": {                                                                                                                                                                   
      "parliamentary_constituency": "E14000845",                                                                                                                                 
      "admin_district": "E07000099",                                                                                                                                             
      "parish": "E04004793",                                                                                                                                                     
      "nuts": "UKH23",                                                                                                                                                           
      "admin_county": "E10000015",                                                                                                                                               
      "ccg": "E38000026",                                                                                                                                                        
      "admin_ward": "E05004782"                                                                                                                                                  
    },                                                                                                                                                                           
    "parliamentary_constituency": "North East Hertfordshire",                                                                                                                    
    "quality": 1,                                                                                                                                                                
    "parish": "Royston",                                                                                                                                                         
    "nuts": "Hertfordshire",                                                                                                                                                     
    "european_electoral_region": "Eastern",                                                                                                                                      
    "latitude": 52.0578463413646,                                                                                                                                                
    "msoa": "North Hertfordshire 002",                                                                                                                                           
    "nhs_ha": "East of England",                                                                                                                                                 
    "primary_care_trust": "Hertfordshire",                                                                                                                                       
    "admin_ward": "Royston Meridian",                                                                                                                                            
    "admin_district": "North Hertfordshire",                                                                                                                                     
    "country": "England",                                                                                                                                                        
    "region": "East of England",                                                                                                                                                 
    "longitude": -0.010476012290143,                                                                                                                                             
    "ccg": "NHS Cambridgeshire and Peterborough",                                                                                                                                
    "lsoa": "North Hertfordshire 002D",                                                                                                                                          
    "northings": 241808                                                                                                                                                          
  }                                                                                                                                                                              
}

这是我的代码:

import sys  
import json
import urllib

def main(argv):
print ("PCinfo Postcode Search")

# Check program invocation is correct
if len(argv) == 2:
    searchterm = argv[1]
else:
    print("Incorrect program call. Usage: PCinfo.py [POSTCODE]")
    sys.exit(0)

url = 'http://api.postcodes.io/postcodes/' 
print ("Calling API with URL " + url + searchterm)

try:
    # For Python 3.0 and later
    from urllib.request import urlopen
    url = url + urllib.parse.quote(searchterm)
    response = urlopen(url).readall().decode('utf-8')
    print("I did 3.0 read")
except ImportError:
    # Fall back to Python 2
    from urllib import urlopen
    url = url + urllib.quote(searchterm)
    response = urlopen(url).read()
    print("I did 2.0 read")

structure = json.loads(response)
print(json.dumps(structure, indent=2))
print(structure["country"])


if __name__== "__main__":
    from sys import argv
    main(argv)

这是程序的输出 . 第一个打印工作正常并显示整个JSON列表,但是当我试图打印出国家时,我得到“关键错误”?花了几个小时看着它!请帮忙!

sh-4.4$ python PCinfo7.py SG87XU                                                                                                                                                 
PCinfo Postcode Search                                                                                                                                                           
Calling API with URL http://api.postcodes.io/postcodes/SG87XU                                                                                                                    
I did 2.0 read                                                                                                                                                                   
{                                                                                                                                                                                
  "status": 200,                                                                                                                                                                 
  "result": {                                                                                                                                                                    
    "eastings": 536500,                                                                                                                                                          
    "outcode": "SG8",                                                                                                                                                            
    "admin_county": "Hertfordshire",                                                                                                                                             
    "postcode": "SG8 7XU",                                                                                                                                                       
    "incode": "7XU",                                                                                                                                                             
    "codes": {                                                                                                                                                                   
      "parliamentary_constituency": "E14000845",                                                                                                                                 
      "admin_district": "E07000099",                                                                                                                                             
      "parish": "E04004793",                                                                                                                                                     
      "nuts": "UKH23",                                                                                                                                                           
      "admin_county": "E10000015",                                                                                                                                               
      "ccg": "E38000026",                                                                                                                                                        
      "admin_ward": "E05004782"                                                                                                                                                  
    },                                                                                                                                                                           
    "parliamentary_constituency": "North East Hertfordshire",                                                                                                                    
    "quality": 1,                                                                                                                                                                
    "parish": "Royston",                                                                                                                                                         
    "nuts": "Hertfordshire",                                                                                                                                                     
    "european_electoral_region": "Eastern",                                                                                                                                      
    "latitude": 52.0578463413646,                                                                                                                                                
    "msoa": "North Hertfordshire 002",                                                                                                                                           
    "nhs_ha": "East of England",                                                                                                                                                 
    "primary_care_trust": "Hertfordshire",                                                                                                                                       
    "admin_ward": "Royston Meridian",                                                                                                                                            
    "admin_district": "North Hertfordshire",                                                                                                                                     
    "country": "England",                                                                                                                                                        
    "region": "East of England",                                                                                                                                                 
    "longitude": -0.010476012290143,                                                                                                                                             
    "ccg": "NHS Cambridgeshire and Peterborough",                                                                                                                                
    "lsoa": "North Hertfordshire 002D",                                                                                                                                          
    "northings": 241808                                                                                                                                                          
  }                                                                                                                                                                              
}                                                                                                                                                                                
Traceback (most recent call last):                                                                                                                                               
  File "PCinfo7.py", line 45, in <module>                                                                                                                                        
    main(argv)                                                                                                                                                                   
  File "PCinfo7.py", line 40, in main                                                                                                                                            
    print(structure["country"])

1 回答

  • 1

    这是因为在 structure json中不存在 country .

    structureresult ,其中包含 country

    使用它访问它

    print(structure['result']['country'])
    

相关问题