我希望将 lat和lng 存储到某个变量中。这是html文件中匹配的模式。我使用正则表达式来匹配该模式,我从页面获取那些匹配的值。
但是如何将 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())
输出:
我可以打印这些值,但我想将这些值存储在某个变量中。
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>
答案 0 :(得分: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'}
>