Reputation: 23
I am trying to print json values in python like below from the json output I receive from the API.
POSTCODE : CB3 0FA COUNTRY : England REGION : East of England
POSTCODE : CB3 0GT COUNTRY : England REGION : East of England
POSTCODE : CB3 0FT COUNTRY : England REGION : East of England
def loadJsonResponse(url):
return json.loads(req.urlopen(url).read().decode('utf-8'))['result']
def nearestPostcode(postcode):
url = 'https://api.postcodes.io/postcodes/{}/nearest'.format(postcode)
value = loadJsonResponse(url)
for i in value['result']:
zip_code = i['postcode']
print (zip_code)
Below is my Json output:
{
"status": 200,
"result": [
{
"postcode": "CB3 0FA",
"quality": 1,
"eastings": 542934,
"northings": 258794,
"country": "England",
"nhs_ha": "East of England",
"longitude": 0.090435,
"latitude": 52.208837,
"european_electoral_region": "Eastern",
"primary_care_trust": "Cambridgeshire",
"region": "East of England",
"lsoa": "Cambridge 007D",
"msoa": "Cambridge 007",
"incode": "0FA",
"outcode": "CB3",
"distance": 0,
"parliamentary_constituency": "Cambridge",
"admin_district": "Cambridge",
"parish": "Cambridge, unparished area",
"admin_county": "Cambridgeshire",
"admin_ward": "Newnham",
"ccg": "NHS Cambridgeshire and Peterborough",
"nuts": "Cambridgeshire CC",
"codes": {
"admin_district": "E07000008",
"admin_county": "E10000003",
"admin_ward": "E05002710",
"parish": "E43000042",
"parliamentary_constituency": "E14000617",
"ccg": "E38000026",
"nuts": "UKH12"
}
},
{
"postcode": "CB3 0GT",
"quality": 1,
"eastings": 542895,
"northings": 258788,
"country": "England",
"nhs_ha": "East of England",
"longitude": 0.089862,
"latitude": 52.208793,
"european_electoral_region": "Eastern",
"primary_care_trust": "Cambridgeshire",
"region": "East of England",
"lsoa": "Cambridge 007D",
"msoa": "Cambridge 007",
"incode": "0GT",
"outcode": "CB3",
"distance": 39.47393492,
"parliamentary_constituency": "Cambridge",
"admin_district": "Cambridge",
"parish": "Cambridge, unparished area",
"admin_county": "Cambridgeshire",
"admin_ward": "Newnham",
"ccg": "NHS Cambridgeshire and Peterborough",
"nuts": "Cambridgeshire CC",
"codes": {
"admin_district": "E07000008",
"admin_county": "E10000003",
"admin_ward": "E05002710",
"parish": "E43000042",
"parliamentary_constituency": "E14000617",
"ccg": "E38000026",
"nuts": "UKH12"
}
},
{
"postcode": "CB3 0FT",
"quality": 1,
"eastings": 542856,
"northings": 258824,
"country": "England",
"nhs_ha": "East of England",
"longitude": 0.089307,
"latitude": 52.209126,
"european_electoral_region": "Eastern",
"primary_care_trust": "Cambridgeshire",
"region": "East of England",
"lsoa": "Cambridge 007D",
"msoa": "Cambridge 007",
"incode": "0FT",
"outcode": "CB3",
"distance": 83.54443275,
"parliamentary_constituency": "Cambridge",
"admin_district": "Cambridge",
"parish": "Cambridge, unparished area",
"admin_county": "Cambridgeshire",
"admin_ward": "Newnham",
"ccg": "NHS Cambridgeshire and Peterborough",
"nuts": "Cambridgeshire CC",
"codes": {
"admin_district": "E07000008",
"admin_county": "E10000003",
"admin_ward": "E05002710",
"parish": "E43000042",
"parliamentary_constituency": "E14000617",
"ccg": "E38000026",
"nuts": "UKH12"
}
}
]
}
I see below error on execution of the script.
Enter the postcode : CB3 0FA
You entered postcode--> CB3 0FA
Traceback (most recent call last):
File "./py_script3.py", line 62, in <module>
nearestPostcode(postcode)
File "./py_script3.py", line 42, in nearestPostcode
for i in value['result']:
TypeError: list indices must be integers or slices, not str
Upvotes: 0
Views: 2069
Reputation: 4606
This should work, using formatted print, tested by loading your json
output
for i in value['result']:
print('POSTCODE: {} England REGION: {}'.format(i['postcode'], i['region']))
POSTCODE: CB3 0FA England REGION: East of England
POSTCODE: CB3 0GT England REGION: East of England
POSTCODE: CB3 0FT England REGION: East of England
Upvotes: 1