尝试从JSON lis打印元素时出现键错误

2024-10-04 05:27:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我有我正在检索的邮政编码数据邮政编码.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"])   

Tags: importurladminmainurllibprinteastcounty