Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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过滤json数据_Python_Json_Dictionary_Filter - Fatal编程技术网

如何使用python过滤json数据

如何使用python过滤json数据,python,json,dictionary,filter,Python,Json,Dictionary,Filter,我试图创建一个函数来过滤从GooglePlacesAPI提取的json数据。如果名称包含字符串“Body Shop”,并且类型为['car_repair'、'point_of_interest'、'Establish'],我希望它返回企业名称和类型值,否则我希望它拒绝结果。这是到目前为止我的代码。我试了又试,但似乎找不到一种方法来存储某些条件以使搜索更容易 import googlemaps import pprint import time import urllib.request

我试图创建一个函数来过滤从GooglePlacesAPI提取的json数据。如果名称包含字符串“Body Shop”,并且类型为['car_repair'、'point_of_interest'、'Establish'],我希望它返回企业名称和类型值,否则我希望它拒绝结果。这是到目前为止我的代码。我试了又试,但似乎找不到一种方法来存储某些条件以使搜索更容易

import googlemaps
import pprint
import time
import urllib.request  






API_KEY = 'YOUR_API_KEY'
lat, lng = 40.35003, -111.95206

#define our Client
gmaps = googlemaps.Client(key = API_KEY)
#Define our Search
places_result = gmaps.places_nearby(location= "40.35003,-111.95206", radius= 40000,open_now= False,type= ['car_repair','point_of_interest','establishment'])
#pprint.pprint(places_result['results'])
time.sleep(3)
places_result_2 = gmaps.places_nearby(page_token = 
places_result['next_page_token'])
pprint.pprint(places_result_2['results'])

places_result_2 = gmaps.places_nearby(page_token = 
places_result['next_page_token'])
    
types = place_details['result']['types']
name = place_details['result']['name']
def match(types,name):
  for val in types: 
      'car_repair','point_of_interest','establishment' in val and "Body Shop" in name
print(name,types)
试试这个:

import googlemaps
import pprint
import time
import urllib.request  




API_KEY = 'YOUR_API_KEY'
lat, lng = 40.35003, -111.95206

#define our Client
gmaps = googlemaps.Client(key = API_KEY)
#Define our Search

places_result = gmaps.places_nearby(location= "40.35003,-111.95206", radius= 40000,open_now= False,type= ['car_repair','point_of_interest','establishment'])


#heres how to retrieve the name of the first result
example_of_name = places_result['results'][0]['name']
print(example_of_name)

#gets places name and type for all the results
for place in places_result['results']:
  print("Name of Place:")
  print(place['name'])
  print("Type of the place:")
  print(place['types'], "\n")
试试这个:

import googlemaps
import pprint
import time
import urllib.request  




API_KEY = 'YOUR_API_KEY'
lat, lng = 40.35003, -111.95206

#define our Client
gmaps = googlemaps.Client(key = API_KEY)
#Define our Search

places_result = gmaps.places_nearby(location= "40.35003,-111.95206", radius= 40000,open_now= False,type= ['car_repair','point_of_interest','establishment'])


#heres how to retrieve the name of the first result
example_of_name = places_result['results'][0]['name']
print(example_of_name)

#gets places name and type for all the results
for place in places_result['results']:
  print("Name of Place:")
  print(place['name'])
  print("Type of the place:")
  print(place['types'], "\n")

非常感谢你!我试试看,没问题!我自己无法复制这个问题,gmaps说模块附近的places_不存在。如果你仍然有这个问题,我可能会帮助进一步提供更多的信息。谢谢!我编辑了代码,添加了从API实际提取json结果的过程。我想我真正想要实现的是在python中的单个变量中存储多个条件,这样我就可以拒绝任何与条件不匹配的数据。我认为Javascript中的一个等价物应该是创建一个const数组,或者至少这是一个不懂python的朋友所说的。问题是过滤器的标准之一应该是“name”,而每个业务名称显然会有所不同,所以我不知道如何存储它(这就是为什么你看到我试图过滤结果,如果他们有字符串“Body Shop”在它们中。但它显然不起作用。嘿,我看到了你的问题。这是因为谷歌在你导入json信息时将其放入一个列表中。这很微妙,但它让你更容易迭代。我已经更新了我的代码,应该可以解决这个问题。非常感谢!我会尝试一下。没问题!我自己无法复制这个问题,gmaps表示places_Nearnear模块不存在。如果您仍然存在问题,我可能可以进一步提供更多信息。谢谢!我编辑了代码,以添加从API实际提取json结果的功能。我想我真正要做的是在python中的单个变量中存储多个条件,以便可以拒绝任何不符合条件的数据sn与标准不匹配。我认为Javascript中的一个等价物应该是创建一个const数组,或者至少这是一个不懂python的朋友所说的。问题是过滤器的标准之一需要是“name”,而每个业务名称显然是不同的,所以我不知道如何存储它(这就是为什么你看到,如果结果中包含字符串“Body Shop”,我会尝试过滤结果。但这显然不起作用。嘿,我看到了你的问题。这是因为谷歌在导入json信息时将其放入列表中。这很微妙,但它让你更容易迭代。我已经更新了代码,这样应该可以解决问题。