2017-08-31 95 views
0

我期待將lat和lng存儲到某個變量中。它是html文件中的匹配模式。我使用了正則表達式來匹配該模式,並且我得到這些匹配值來自頁面。如何在變量中存儲match.groups()值

但我怎樣才能將的match.groups()值存儲到變量

這是我的代碼。

import re 

pattern = re.compile("(l[an][gt])(:\s+)(\d+\.\d+)") 
for i, line in enumerate(open('C:\hile_text.html')): 
    for match in re.finditer(pattern, line): 
     print 'Found on line %s: %s' % (i+1, match.groups()) 

輸出:

  • 實測值線3218:( 'LAT', ':','21 0.244791 ')
  • 實測值線3218:(' LNG」, ':', '81 .649491')

我能夠打印這些值,但我想將該值存儲在某個變量中。

Plzz幫助我。

這裏是文件

<script type="text/javascript"> 
    window.mapDivId = 'map0Div'; 
    window.map0Div = { 
    lat: 21.25335, 
    lng: 81.649445, 
    zoom: null, 
    locId: 5897747, 
    geoId: 297595, 
    isAttraction: false, 
    isEatery: true, 
    isLodging: false, 
    isNeighborhood: false, 
    title: "Aman Age Roll & Chicken ", 
    homeIcon: true, 
    url: "/Restaurant_Review-g297595-d5897747-Reviews-Aman_Age_Roll_Chicken-Raipur_Raipur_District_Chhattisgarh.html", 
    minPins: [ 
    ['hotel', 20], 
    ['restaurant', 20], 
    ['attraction', 20], 
    ['vacation_rental', 0]  ], 
    units: 'km', 
    geoMap: false, 
    tabletFullSite: false, 
    reuseHoverDivs: false, 
    noSponsors: true }; 
    ta.store('infobox_js', 'https://static.tacdn.com/js3/infobox-c-v21051733989b.js'); 
    ta.store("ta.maps.apiKey", ""); 
    (function() { 
    var onload = function() { 
    if (window.location.hash == "#MAPVIEW") { 
    ta.run("ta.mapsv2.Factory.handleHashLocation", {}, true); 
    } 
    } 
    if (window.addEventListener) { 
    if (window.history && window.history.pushState) { 
    window.addEventListener("popstate", function(e) { 
    ta.run("ta.mapsv2.Factory.handleHashLocation", {}, false); 
    }, false); 
    } 
    window.addEventListener('load', onload, false); 
    } 
    else if (window.attachEvent) { 
    window.attachEvent('onload', onload); 
    } 
    })(); 
    ta.store("mapsv2.show_sidebar", true); 
    ta.store('mapsv2_restaurant_reservation_js', ["https://static.tacdn.com/js3/ta-mapsv2-restaurant-reservation-c-v2430632369b.js"]); 
    ta.store('mapsv2.typeahead_css', "https://static.tacdn.com/css2/maps_typeahead-v21940478230b.css"); 
    // Feature gate VR price pins on SRP map. VRC-14803 
    ta.store('mapsv2.vr_srp_map_price_enabled', true); 
    ta.store('mapsv2.geoName', 'Raipur'); 
    ta.store('mapsv2.map_addressnotfound', "Address not found");  ta.store('mapsv2.map_addressnotfound3', "We couldn\'t find that location near {0}. Please try another search.");  ta.store('mapsv2.directions', "Directions from {0} to {1}");  ta.store('mapsv2.enter_dates', "Enter dates for best prices");  ta.store('mapsv2.best_prices', "Best prices for your stay");  ta.store('mapsv2.list_accom', "List of accommodations");  ta.store('mapsv2.list_hotels', "List of hotels");  ta.store('mapsv2.list_vrs', "List of holiday rentals");  ta.store('mapsv2.more_accom', "More accommodations");  ta.store('mapsv2.more_hotels', "More hotels");  ta.store('mapsv2.more_vrs', "More Holiday Homes");  ta.store('mapsv2.sold_out_on_1', "SOLD OUT on 1 site");  ta.store('mapsv2.sold_out_on_y', "SOLD OUT on 2 sites"); </script> 

回答

1

會像下面這樣做你需要什麼?

import re 

latitude = r"lat:\s+(\d+\.\d+)" 
longitude = r"lng:\s+(\d+\.\d+)" 

for i, line in enumerate(open('C:\hile_text.html'), start=1): 
    match = re.search(latitude, line) 
    if match: 
     lat = match.group(1) 
     print('Found latitude on line %s: %s' % (i, lat)) 

    match = re.search(longitude, line) 
    if match: 
     lng = match.group(1) 
     print('Found longitude on line %s: %s' % (i, lng)) 

輸出

> python test.py 
Found latitude on line 4: 21.25335 
Found longitude on line 5: 81.649445 
> 

如果你想保留一個單一模式的做法,我們可以存儲在字典中的兩個值:

import re 

pattern = r"(lat|lng):\s+(\d+\.\d+)" 

location = {} 

for i, line in enumerate(open('C:\hile_text.html'), start=1): 
    match = re.search(pattern, line) 
    if match: 
     key, value = match.group(1, 2) 
     location[key] = value 
     print('Found %s on line %s: %s' % (key, i, value)) 

print(location) 

輸出

> python test.py 
Found lat on line 4: 21.25335 
Found lng on line 5: 81.649445 
{'lat': '21.25335', 'lng': '81.649445'} 
>