解析JSON文件但未返回所有内容

时间:2018-06-01 13:52:30

标签: python json parsing google-places-api

我正在尝试从Google Places API解析JSON文件。我正在尝试收集附近地址,lng / lat坐标以及地点ID。在包含JSON文件的页面上,我的代码捕获了更多位置信息,但由于某种原因,它没有从其中一个位置获取数据。我的代码是:

response = urllib.urlopen(url)
data = json.loads(response.read())
for n in range(len(data['results'])):
  location = []
  address = data['results'][n]['vicinity']
  address = address[: address.find(", ")]
  coords_lat = str(data['results'][n]['geometry']['location']['lat'])
  coords_lat = coords_lat[:-4]
  coords_lng = str(data['results'][n]['geometry']['location']['lng'])
  coords_lng = coords_lng[:-4]
  place_id = data['results'][n]['place_id']
  location = [address, coords_lat, coords_lng, place_id]
  print location

但是对于其中一个地点,我的程序正在返回: ...[u'C', '33.87', '-84.53', u'ChIJOUmV_jgX9YgRdN1sudI8yOI']...代替此地点的完整附近地址'C, 2976 Ask-Kay Dr, Smyrna'

This is my python program

This is the API JSON page with one of the results I'm referring to

These are some of the lines my program is printing

1 个答案:

答案 0 :(得分:0)

您可能希望在

之后立即显示address
address = data['results'][n]['vicinity']
在使用

编辑之前

address = address[: address.find(", ")]

可能与文本中的,字符

有关
  

C,2976 Ask-Kay博士,士麦那