2017-08-31 135 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'} 
>