如何存储匹配.组()变量值

2024-10-02 16:21:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我想把lat和lng储存在变量。哪个是html文件中的匹配模式。我使用正则表达式匹配该模式,并从页面中获取匹配值。你知道吗

但我如何存储匹配.组()变量值???你知道吗

这是我的密码。你知道吗

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.244791')
  • 在第3218行找到:('lng'、':'、'81.649491')

我可以打印这些值,但我想将这些值存储在某个变量中。你知道吗

请帮帮我。你知道吗

这是文件

<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>

Tags: storefalsetruemapforifonjs
1条回答
网友
1楼 · 发布于 2024-10-02 16:21:51

像下面这样的东西能满足你的需要吗?你知道吗

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'}
> 

相关问题 更多 >