2017-03-15 42 views
0

我有一個大的字符串(稱爲LargeSTR),它具有以下模式的多個實例的字符串模式:如何找到在python

{"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}, 
+0

這不是json文件嗎?如果你分享整個文件或文件的適當部分,那麼它會更容易分辨。 – smttsp

+0

不,它是一個文本文件,來自lxml.html和請求。我只需要能夠從中提取這些作品: {「位置」:其中A和B可能是不同的浮點數 –

+0

說你做提取這些值B}, :{「緯度」:一,「經度」從大字符串中,你將如何將它們組織成有用的東西,或者知道經度和緯度的每個值都屬於哪些元素?將字符串解析爲字典或將其加載到json對象中會使其更易於管理。 – davedwards

回答

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'] 
+0

感謝您給我們的一個非常簡潔的方式,能否請你告訴我如何獲得和使用JSON文件?是的,這是yelp的數據。 –

+0

編輯答案。請檢查 – smttsp