2017-05-07 80 views
-6

我需要從文件中提取後的代碼,我從谷歌文檔一樣下載:正則表達式來查找郵編?

{ 
    'address_components':[ 
     { 
     'long_name':'Orrs Walk', 
     'short_name':'Orrs Walk', 
     'types':[ 
      'route' 
     ] 
     }, 
     { 
     'long_name':'South Wharf', 
     'short_name':'South Wharf', 
     'types':[ 
      'locality', 
      'political' 
     ] 
     }, 
     { 
     'long_name':'Melbourne City', 
     'short_name':'Melbourne', 
     'types':[ 
      'administrative_area_level_2', 
      'political' 
     ] 
     }, 
     { 
     'long_name':'Victoria', 
     'short_name':'VIC', 
     'types':[ 
      'administrative_area_level_1', 
      'political' 
     ] 
     }, 
     { 
     'long_name':'Australia', 
     'short_name':'AU', 
     'types':[ 
      'country', 
      'political' 
     ] 
     }, 
     { 
     'long_name':'3006', 
     'short_name':'3006', 
     'types':[ 
      'postal_code' 
     ] 
     } 
    ], 
    'formatted_address':'Orrs Walk, South Wharf VIC 3006, Australia', 
    'geometry':{ 
     'bounds':{ 
     'northeast':{ 
      'lat':-37.8236993, 
      'lng':144.9560253 
     }, 
     'southwest':{ 
      'lat':-37.8250865, 
      'lng':144.9523143 
     } 
     }, 
     'location':{ 
     'lat':-37.8247832, 
     'lng':144.9541924 
     }, 
     'location_type':'GEOMETRIC_CENTER', 
     'viewport':{ 
     'northeast':{ 
      'lat':-37.8230439197085, 
      'lng':144.9560253 
     }, 
     'southwest':{ 
      'lat':-37.8257418802915, 
      'lng':144.9523143 
     } 
     } 
    }, 
    'place_id':'ChIJb1iRdFdd1moReZ57m5XraDk', 
    'types':[ 
     'route' 
    ] 
}, 
{ 
    'address_components':[ 
     { 
     'long_name':'South Wharf', 
     'short_name':'South Wharf', 
     'types':[ 
      'locality', 
      'political' 
     ] 
     }, 
     { 
     'long_name':'Melbourne City', 
     'short_name':'Melbourne', 
     'types':[ 
      'administrative_area_level_2', 
      'political' 
     ] 
     }, 
     { 
     'long_name':'Victoria', 
     'short_name':'VIC', 
     'types':[ 
      'administrative_area_level_1', 
      'political' 
     ] 
     }, 
     { 
     'long_name':'Australia', 
     'short_name':'AU', 
     'types':[ 
      'country', 
      'political' 
     ] 
     }, 
     { 
     'long_name':'3006', 
     'short_name':'3006', 
     'types':[ 
      'postal_code' 
     ] 
     } 
    ], 
    'formatted_address':'South Wharf VIC 3006, Australia', 
    'geometry':{ 
     'bounds':{ 
     'northeast':{ 
      'lat':-37.8230788, 
      'lng':144.9566558 
     }, 
     'southwest':{ 
      'lat':-37.8276482, 
      'lng':144.9475157 
     } 
     }, 
     'location':{ 
     'lat':-37.825, 
     'lng':144.952 
     }, 
     'location_type':'APPROXIMATE', 
     'viewport':{ 
     'northeast':{ 
      'lat':-37.8230788, 
      'lng':144.9566558 
     }, 
     'southwest':{ 
      'lat':-37.8276482, 
      'lng':144.9475157 
     } 
     } 
    }, 
    'place_id':'ChIJ1YQ5tfdn1moRAAGNIXVWBAU', 
    'types':[ 
     'locality', 
     'political' 
    ] 
}, 
{ 
    'address_components':[ 
     { 
     'long_name':'Melbourne', 
     'short_name':'Melbourne', 
     'types':[ 
      'colloquial_area', 
      'locality', 
      'political' 
     ] 
     }, 
     { 
     'long_name':'Victoria', 
     'short_name':'VIC', 
     'types':[ 
      'administrative_area_level_1', 
      'political' 
     ] 
     }, 
     { 
     'long_name':'Australia', 
     'short_name':'AU', 
     'types':[ 
      'country', 
      'political' 
     ] 
     } 
    ], 
    'formatted_address':'Melbourne VIC, Australia', 
    'geometry':{ 
     'bounds':{ 
     'northeast':{ 
      'lat':-37.5112737, 
      'lng':145.5125288 
     }, 
     'southwest':{ 
      'lat':-38.4338593, 
      'lng':144.5937418 
     } 
     }, 
     'location':{ 
     'lat':-37.8136276, 
     'lng':144.9630576 
     }, 
     'location_type':'APPROXIMATE', 
     'viewport':{ 
     'northeast':{ 
      'lat':-37.5112737, 
      'lng':145.5125288 
     }, 
     'southwest':{ 
      'lat':-38.4338593, 
      'lng':144.5937418 
     } 
     } 
    }, 
    'place_id':'ChIJ90260rVG1moRkM2MIXVWBAQ', 
    'types':[ 
     'colloquial_area', 
     'locality', 
     'political' 
    ] 
}, 
{ 
    'address_components':[ 
     { 
     'long_name':'3006', 
     'short_name':'3006', 
     'types':[ 
      'postal_code' 
     ] 
     }, 
     { 
     'long_name':'South Wharf', 
     'short_name':'South Wharf', 
     'types':[ 
      'locality', 
      'political' 
     ] 
     }, 
     { 
     'long_name':'Victoria', 
     'short_name':'VIC', 
     'types':[ 
      'administrative_area_level_1', 
      'political' 
     ] 
     }, 
     { 
     'long_name':'Australia', 
     'short_name':'AU', 
     'types':[ 
      'country', 
      'political' 
     ] 
     } 
    ], 
    'formatted_address':'South Wharf VIC 3006, Australia', 
    'geometry':{ 
     'bounds':{ 
     'northeast':{ 
      'lat':-37.8192449, 
      'lng':144.971423 
     }, 
     'southwest':{ 
      'lat':-37.8314821, 
      'lng':144.946908 
     } 
     }, 
     'location':{ 
     'lat':-37.8245483, 
     'lng':144.963937 
     }, 
     'location_type':'APPROXIMATE', 
     'viewport':{ 
     'northeast':{ 
      'lat':-37.8192449, 
      'lng':144.971423 
     }, 
     'southwest':{ 
      'lat':-37.8314821, 
      'lng':144.946908 
     } 
     } 
    }, 
    'place_id':'ChIJb3SLlEdd1moR0DkuRnhWBBw', 
    'postcode_localities':[ 
     'South Wharf', 
     'Southbank' 
    ], 
    'types':[ 
     'postal_code' 
    ] 
}, 
{ 
    'address_components':[ 
     { 
     'long_name':'Melbourne City', 
     'short_name':'Melbourne', 
     'types':[ 
      'administrative_area_level_2', 
      'political' 
     ] 
     }, 
     { 
     'long_name':'Victoria', 
     'short_name':'VIC', 
     'types':[ 
      'administrative_area_level_1', 
      'political' 
     ] 
     }, 
     { 
     'long_name':'Australia', 
     'short_name':'AU', 
     'types':[ 
      'country', 
      'political' 
     ] 
     } 
    ], 
    'formatted_address':'Melbourne, VIC, Australia', 
    'geometry':{ 
     'bounds':{ 
     'northeast':{ 
      'lat':-37.7754505, 
      'lng':144.9913306 
     }, 
     'southwest':{ 
      'lat':-37.8506672, 
      'lng':144.8969813 
     } 
     }, 
     'location':{ 
     'lat':-37.8100512, 
     'lng':144.9281496 
     }, 
     'location_type':'APPROXIMATE', 
     'viewport':{ 
     'northeast':{ 
      'lat':-37.7754505, 
      'lng':144.9913306 
     }, 
     'southwest':{ 
      'lat':-37.8506672, 
      'lng':144.8969813 
     } 
     } 
    }, 
    'place_id':'ChIJv_FYgkNd1moRpxLuRXZURFs', 
    'types':[ 
     'administrative_area_level_2', 
     'political' 
    ] 
}, 
{ 
    'address_components':[ 
     { 
     'long_name':'CBD & South Melbourne', 
     'short_name':'CBD & South Melbourne', 
     'types':[ 
      'political' 
     ] 
     }, 
     { 
     'long_name':'Melbourne', 
     'short_name':'Melbourne', 
     'types':[ 
      'colloquial_area', 
      'locality', 
      'political' 
     ] 
     }, 
     { 
     'long_name':'Victoria', 
     'short_name':'VIC', 
     'types':[ 
      'administrative_area_level_1', 
      'political' 
     ] 
     }, 
     { 
     'long_name':'Australia', 
     'short_name':'AU', 
     'types':[ 
      'country', 
      'political' 
     ] 
     } 
    ], 
    'formatted_address':'CBD & South Melbourne, Melbourne VIC, Australia', 
    'geometry':{ 
     'bounds':{ 
     'northeast':{ 
      'lat':-37.7730008, 
      'lng':145.0158347 
     }, 
     'southwest':{ 
      'lat':-37.8574821, 
      'lng':144.8969813 
     } 
     }, 
     'location':{ 
     'lat':-37.8362164, 
     'lng':144.9501708 
     }, 
     'location_type':'APPROXIMATE', 
     'viewport':{ 
     'northeast':{ 
      'lat':-37.7730008, 
      'lng':145.0158347 
     }, 
     'southwest':{ 
      'lat':-37.8574821, 
      'lng':144.8969813 
     } 
     } 
    }, 
    'place_id':'ChIJORuuCkxd1moRNMrml7yk-C8', 
    'types':[ 
     'political' 
    ] 
}, 
{ 
    'address_components':[ 
     { 
     'long_name':'Melbourne Metropolitan Area', 
     'short_name':'Melbourne Metropolitan Area', 
     'types':[ 
      'political' 
     ] 
     }, 
     { 
     'long_name':'Victoria', 
     'short_name':'VIC', 
     'types':[ 
      'administrative_area_level_1', 
      'political' 
     ] 
     }, 
     { 
     'long_name':'Australia', 
     'short_name':'AU', 
     'types':[ 
      'country', 
      'political' 
     ] 
     } 
    ], 
    'formatted_address':'Melbourne Metropolitan Area, VIC, Australia', 
    'geometry':{ 
     'bounds':{ 
     'northeast':{ 
      'lat':-37.4017502, 
      'lng':146.1925247 
     }, 
     'southwest':{ 
      'lat':-38.4999344, 
      'lng':144.4440773 
     } 
     }, 
     'location':{ 
     'lat':-37.8001063, 
     'lng':145.3143491 
     }, 
     'location_type':'APPROXIMATE', 
     'viewport':{ 
     'northeast':{ 
      'lat':-37.4017502, 
      'lng':146.1925247 
     }, 
     'southwest':{ 
      'lat':-38.4999344, 
      'lng':144.4440773 
     } 
     } 
    }, 
    'place_id':'ChIJmYjB3BaF1moRtCmV8wIoZQU', 
    'types':[ 
     'political' 
    ] 
}, 
{ 
    'address_components':[ 
     { 
     'long_name':'Victoria', 
     'short_name':'VIC', 
     'types':[ 
      'administrative_area_level_1', 
      'political' 
     ] 
     }, 
     { 
     'long_name':'Australia', 
     'short_name':'AU', 
     'types':[ 
      'country', 
      'political' 
     ] 
     } 
    ], 
    'formatted_address':'Victoria, Australia', 
    'geometry':{ 
     'bounds':{ 
     'northeast':{ 
      'lat':-33.9806474, 
      'lng':150.0169685 
     }, 
     'southwest':{ 
      'lat':-39.18316069999999, 
      'lng':140.9616819 
     } 
     }, 
     'location':{ 
     'lat':-37.4713077, 
     'lng':144.7851531 
     }, 
     'location_type':'APPROXIMATE', 
     'viewport':{ 
     'northeast':{ 
      'lat':-33.9806474, 
      'lng':149.9764884 
     }, 
     'southwest':{ 
      'lat':-39.1590935, 
      'lng':140.9616819 
     } 
     } 
    }, 
    'place_id':'ChIJT5UYfksx1GoRNJWCvuL8Tlo', 
    'types':[ 
     'administrative_area_level_1', 
     'political' 
    ] 
}, 
{ 
    'address_components':[ 
     { 
     'long_name':'Australia', 
     'short_name':'AU', 
     'types':[ 
      'country', 
      'political' 
     ] 
     } 
    ], 
    'formatted_address':'Australia', 
    'geometry':{ 
     'bounds':{ 
     'northeast':{ 
      'lat':-9.187026399999999, 
      'lng':159.2872223 
     }, 
     'southwest':{ 
      'lat':-54.83376579999999, 
      'lng':110.9510339 
     } 
     }, 
     'location':{ 
     'lat':-25.274398, 
     'lng':133.775136 
     }, 
     'location_type':'APPROXIMATE', 
     'viewport':{ 
     'northeast':{ 
      'lat':-0.6911343999999999, 
      'lng':166.7429167 
     }, 
     'southwest':{ 
      'lat':-51.66332320000001, 
      'lng':100.0911072 
     } 
     } 
    }, 
    'place_id':'ChIJ38WHZwf9KysRUhNblaFnglM', 
    'types':[ 
     'country', 
     'political' 
    ] 
} 
] 

我需要提取郵政編碼,'short_name': '3006',但不是所有的SHORT_NAME緊隨其後,郵編

我已經厭倦了某些方面,但我只是獲得了一些空間,我用了帶字

import re 

hand = open('data') 

for line in hand: 
    #print line 
    line = line.rstrip() 

    stuff = re.findall('^\'short_name\':([0-9]+)',line) 
    print stuff 

和我得到的結果是這樣的:

[] 

Process finished with exit code 0 

我固定它,這樣的:它們之間存在太多的空間,

import re 

hand = open('data') 
for line in hand: 
    line = line.rstrip() 

    stuff = re.findall(r'\'short_name\':\s*\'([0-9]{4})\'',line) 
    #stuff = re.findall(r'\'short_name\':([0-9]{4})',line) 
    print stuff 
+2

所以。你有什麼嘗試? – kuro

+2

爲什麼不直接解析json? – luoluo

+0

你嘗試過什麼嗎?請顯示你的嘗試。 – bhansa

回答

-3
'[a-z]++_[a-z]++':'([0-9]++)' 

注意是否存在任何中旬空間。

+0

地球上這是如何回答? – DyZ

0

你正在處理的數據看起來像JSON,但它不是 - 因爲單引號(JSON需要雙引號)。

假設fileContent是你的文件的內容(字符串,如你的問題所示),你可以將其轉換爲一個Python數據結構,然後搜索爲詞典列表:

import ast 
data = ast.literal_eval(fileContent) 
codes = [[field['short_name'] for field in entry['address_components'] 
      if 'postal_code' in field['types']] 
     for entry in data] 
#[['3006'], ['3006'], [], ['3006'], [], [], [], [], []] 

你可以從該列表中提取任何其他信息。