如何在python中找到字符串模式

时间:2017-03-15 23:54:16

标签: regex string python-2.7 search

我有一个大字符串(称为LargeSTR),它有多个以下模式的实例:

{"location": {"latitude": 34.04481184664412, "longitude": -118.30783339686604},

在每个实例中,纬度和经度的值可能不同,我如何通过搜索模式从这个大字符串中提取这些纬度和经度数据。这是LargSTR的一个例子

, "hovercard_id": "1Lm6ExY6B8v9E6B09j5h6w", "resource_type": "business", "icon": {"regular_uri": "https://media2.fl.yelpcdn.com/mapmarkers/yelp_map_range/20160801/21/30.png", "scaled_size": [48, 320], "anchor_offset": [12, 32], "name": "business", "active_origin": [24, 0], "active_uri": "https://media2.fl.yelpcdn.com/mapmarkers/yelp_map_range/20160801/21/30.png", "regular_origin": [0, 0], "size": [24, 32]}}, "22": {"url": "/biz/la-barca-restaurant-los-angeles", "location": {"latitude": 34.0339201576781, "longitude": -118.29148504989}, "key": 22, "hovercard_id": "B68Kl37vf9qF7GImxhELkQ", "resource_type": "business", "icon": {"regular_uri": "https://media2.fl.yelpcdn.com/mapmarkers/yelp_map_range/20160801/21/30.png", "scaled_size": [48, 320], "anchor_offset": [12, 32], "name": "business", "active_origin": [24, 32], "active_uri": "https://media2.fl.yelpcdn.com/mapmarkers/yelp_map_range/20160801/21/30.png", "regular_origin": [0, 32], "size": [24, 32]}}, "23": {"url": "/biz/el-migueleno-restaurant-los-angeles", "location": {"latitude": 34.0350506, "longitude": -118.2915704}, "key": 23, "hovercard_id": "y3815rRI1BP66oq09m6TjQ", "resource_type": "business", "icon": {"regular_uri": "https://media2.fl.yelpcdn.com/mapmarkers/yelp_map_range/20160801/21/30.png", "scaled_size": [48, 320], "anchor_offset": [12, 32], "name": "business", "active_origin": [24, 64], "active_uri": "https://media2.fl.yelpcdn.com/mapmarkers/yelp_map_range/20160801/21/30.png", "regular_origin": [0, 64], "size": [24, 32]}}, "24": {"url": "/biz/house-of-curry-los-angeles-4", "location": {"latitude": 34.040289, "longitude": -118.3086312}, "key": 24, "hovercard_id": "SoisYg5oSRA5eiBl0wPALg", "resource_type": "business", "icon": {"regular_uri": "https://media2.fl.yelpcdn.com/mapmarkers/yelp_map_range/20160801/21/30.png", "scaled_size": [48, 320], "anchor_offset": [12, 32], "name": "business", "active_origin": [24, 96], "active_uri": "https://media2.fl.yelpcdn.com/mapmarkers/yelp_map_range/20160801/21/30.png", "regular_origin": [0, 96], "size": [24, 32]}}, "25": {"url": "/biz/ebaes-los-angeles", "location": {"latitude": 34.0343956, "longitude": -118.2836649}, "key": 25, "hovercard_id": "Tnc0HTOhLEWEXAPBjH5u7w", "resource_type": "business", "icon": {"regular_uri": "https://media2.fl.yelpcdn.com/mapmarkers/yelp_map_range/20160801/21/30.png", "scaled_size": [48, 320], "anchor_offset": [12, 32], "name": "business", "active_origin": [24, 128], "active_uri": "https://media2.fl.yelpcdn.com/mapmarkers/yelp_map_range/20160801/21/30.png", "regular_origin": [0, 128], "size": [24, 32]}}, "26": {"url": "/biz/belizean-fish-market-los-angeles-2", "location": {"latitude": 34.0314363, "longitude": -118.3087631}, "key": 26, "hovercard_id": "NnO4zEpZt3MvzYFU_MrXaQ", "resource_type": "business", "icon": {"regular_uri": "https://media2.fl.yelpcdn.com/mapmarkers/yelp_map_range/20160801/21/30.png", "scaled_size": [48, 320], "anchor_offset": [12, 32], "name": "business", "active_origin": [24, 160], "active_uri": "https://media2.fl.yelpcdn.com/mapmarkers/yelp_map_range/20160801/21/30.png", "regular_origin": [0, 160], "size": [24, 32]}}, "27": {"url": "/biz/omars-hot-dog-stand-los-angeles", "location": {"latitude": 34.0286688, "longitude": -118.2915386}, "key": 27, "hovercard_id": "9hcR6H1PePDMB_JziH7VSA", "resource_type": "business", "icon": {"regular_uri": "https://media2.fl.yelpcdn.com/mapmarkers/yelp_map_range/20160801/21/30.png", "scaled_size": [48, 320], "anchor_offset": [12, 32], "name": "business", "active_origin": [24, 192], "active_uri": "https://media2.fl.yelpcdn.com/mapmarkers/yelp_map_range/20160801/21/30.png", "regular_origin": [0, 192], "size": [24, 32]}}, "28": {"url": "/biz/bacaro-la-los-angeles", "location": {"latitude": 34.034478, "longitude": -118.28342}, "key": 28, "hovercard_id": "0UCg5MwkhM4Qj1GnFdXBFA", "resource_type": "business", "icon": {"regular_uri": "https://media2.fl.yelpcdn.com/mapmarkers/yelp_map_range/20160801/21/30.png", "scaled_size": [48, 320], "anchor_offset": [12, 32], "name": "business", "active_origin": [24, 224], "active_uri": "https://media2.fl.yelpcdn.com/mapmarkers/yelp_map_range/20160801/21/30.png", "regular_origin": [0, 224], "size": [24, 32]}}, "29": {"url": "/biz/teds-burger-3-los-angeles", "location": {"latitude": 34.0258372, "longitude": -118.3005593}, "key": 29, "hovercard_id": "eETHUFJ7cCC8mrQGUiJMpQ", "resource_type": "business", "icon": {"regular_uri": "https://media2.fl.yelpcdn.com/mapmarkers/yelp_map_range/20160801/21/30.png", "scaled_size": [48, 320], "anchor_offset": [12, 32], "name": "business", "active_origin": [24, 256], "active_uri": "https://media2.fl.yelpcdn.com/mapmarkers/yelp_map_range/20160801/21/30.png", "regular_origin": [0, 256], "size": [24, 32]}}, "30": {"url": "/biz/study-hall-los-angeles-3", "location": {"latitude": 34.028543, "longitude": -118.284365}, "key": 30, "hovercard_id": "nrgJNUjt0vNF7TrhmWMtiw", "resource_type": "business", "icon": {"regular_uri": "https://media2.fl.yelpcdn.com/mapmarkers/yelp_map_range/20160801/21/30.png", "scaled_size": [48, 320], "anchor_offset": [12, 32], "name": "business", "active_origin": [24, 288], "active_uri": "https://media2.fl.yelpcdn.com/mapmarkers/yelp_map_range/20160801/21/30.png", "regular_origin": [0, 288], "size": [24, 32]}}}, "top_biz_bounds": null, "hoods": [{"highlighted": false, "text": {"location": {"latitude": 34.04481184664412, "longitude": -118.30783339686604}, 

1 个答案:

答案 0 :(得分:0)

以下代码将为您提供所有locations

import re
[a.start() for a in re.finditer('"location"', mystr)]

输出结果为:[x1 x2 x3 ... xn]其中xi"location"mystr的第一个字符。

然后,对于返回的每个值,您可以在第一个{}之间获取数据。

请注意,此数据类似于 Yelp业务数据集。我以前玩过这些数据,我记得有一些json文件,你可以直接访问每个业务的位置信息而无需搜索字符串。

编辑Yelp营业日期 据我所知,您正在查看的列表是一个词典列表。

您将首先将整个文件读入列表。每个企业都有一个要素。

 for busn in list:
      busn['location'] -> this will give you the location dictionary
      busn['location']['lattitude'] will give you lattitude of the business.

您可能需要检查商家是否有位置信息。你可以这样做:

 if 'location' in busn: 
        busn['location']