[英]How to extract longitude and latitude from a link
From the following link, I am trying to extract the longitude and latitude. 从以下链接,我试图提取经度和纬度。 I found similar posts but not one with the same format.
我找到了类似的帖子,但没有找到相同格式的帖子。 I'm new to regex/text manipulation and would appreciate any guidance on how I might do this using Python.
我是正则表达式/文本操作的新手,并且非常感谢有关如何使用Python进行此操作的任何指导。 The output I'd like to have from this example is
我想从这个例子得到的输出是
latitude = 40.744221
longitude = -73.982854
Many thanks in advance. 提前谢谢了。
https://maps.googleapis.com/maps/api/staticmap?scale=1¢er=40.744221%2C-73.982854&language=en&zoom=15&markers=scale%3A1%7Cicon%3Ahttps%3A%2F%2Fyelp-images.s3.amazonaws.com%2Fassets%2Fmap-markers%2Fannotation_32x43.png%7C40.744221%2C-73.982854&client=gme-yelp&sensor=false&size=315x150&signature=OjixVjNCwF7yLR5tsYw2fDRZ7bw
https://maps.googleapis.com/maps/api/staticmap?scale=1¢er=40.744221%2C-73.982854&language=en&zoom=15&markers=scale%3A1%7Cicon%3Ahttps%3A%2F%2Fyelp-images.s3.amazonaws .COM%2Fassets%2Fmap标志物%2Fannotation_32x43.png%7C40.744221%2C-73.982854&客户= GME-喊叫&传感器=假大小= 315x150&签名= OjixVjNCwF7yLR5tsYw2fDRZ7bw
Python has a module for parsing URLs in the standard library Python有一个用于解析标准库中的URL的模块
from urllib import parse
# Split off the query
_, query_string = parse.splitquery("https://maps.googleapis.com/maps/api/staticmap?scale=1¢er=40.744221%2C-73.982854&language=en&zoom=15&markers=scale%3A1%7Cicon%3Ahttps%3A%2F%2Fyelp-images.s3.amazonaws.com%2Fassets%2Fmap-markers%2Fannotation_32x43.png%7C40.744221%2C-73.982854&client=gme-yelp&sensor=false&size=315x150&signature=OjixVjNCwF7yLR5tsYw2fDRZ7bw")
# Parse the query into a dict
query = parse.parse_qs(query_string)
# You can now access the query using a dict lookup
latlng = query["center"]
# And to get the values (selecting 0 as it is valid for a query string to contain the same key multiple times).
latitude, longitude = latlng[0].split(",")
For this usecase I would avoid regular expressions. 对于这个用例,我会避免使用正则表达式。 The
urllib
module is more explicit, will handle all aspects of URL encoding and is well tested. urllib
模块更明确,将处理URL编码的所有方面,并经过充分测试。
Another great third party module for handling URLs is the excellent YARL . 另一个用于处理URL的优秀第三方模块是优秀的YARL 。
Use a simple re.search
on the string with tuple packing: 对包含元组的字符串使用简单的
re.search
:
lattitude, longitude = re.search(r'center=(.*?)%2C(.*?)&', s).groups()
where s
is your string (link). 其中
s
是你的字符串(链接)。
Example : 示例 :
import re
s = 'https://maps.googleapis.com/maps/api/staticmap?scale=1¢er=40.744221%2C-73.982854&language=en&zoom=15&markers=scale%3A1%7Cicon%3Ahttps%3A%2F%2Fyelp-images.s3.amazonaws.com%2Fassets%2Fmap-markers%2Fannotation_32x43.png%7C40.744221%2C-73.982854&client=gme-yelp&sensor=false&size=315x150&signature=OjixVjNCwF7yLR5tsYw2fDRZ7bw'
lattitude, longitude = re.search(r'center=(.*?)%2C(.*?)&', s).groups()
print(lattitude) # 40.744221
print(longitude) # -73.982854
I'm guessing that this expression might return our desired output: 我猜这个表达式可能会返回我们想要的输出:
center=(-?\d+\.\d+)%2C(-?\d+\.\d+)
re.findall
re.findall
测试 import re
regex = r"center=(-?\d+\.\d+)%2C(-?\d+\.\d+)"
test_str = "https://maps.googleapis.com/maps/api/staticmap?scale=1¢er=40.744221%2C-73.982854&language=en&zoom=15&markers=scale%3A1%7Cicon%3Ahttps%3A%2F%2Fyelp-images.s3.amazonaws.com%2Fassets%2Fmap-markers%2Fannotation_32x43.png%7C40.744221%2C-73.982854&client=gme-yelp&sensor=false&size=315x150&signature=OjixVjNCwF7yLR5tsYw2fDRZ7bw"
print(re.findall(regex, test_str))
re.finditer
re.finditer
测试 import re
regex = r"center=(-?\d+\.\d+)%2C(-?\d+\.\d+)"
test_str = "https://maps.googleapis.com/maps/api/staticmap?scale=1¢er=40.744221%2C-73.982854&language=en&zoom=15&markers=scale%3A1%7Cicon%3Ahttps%3A%2F%2Fyelp-images.s3.amazonaws.com%2Fassets%2Fmap-markers%2Fannotation_32x43.png%7C40.744221%2C-73.982854&client=gme-yelp&sensor=false&size=315x150&signature=OjixVjNCwF7yLR5tsYw2fDRZ7bw"
matches = re.finditer(regex, test_str, re.MULTILINE)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
The expression is explained on the top right panel of this demo , if you wish to explore/simplify/modify it, and in this link , you can watch how it would match against some sample inputs step by step, if you like. 如果您希望探索/简化/修改该演示文稿 ,可以在本演示的右上方面板中解释该表达式,如果您愿意,可以在此链接中逐步观察它与一些示例输入的匹配情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.