从以下链接中,我试图提取经度和纬度。我发现了类似的帖子,但没有找到具有相同格式的帖子。我是regex /文本操作的新手,并且希望获得有关如何使用Python进行此操作的任何指导。我想从此示例获得的输出是
latitude = 40.744221
longitude = -73.982854
非常感谢。
答案 0 :(得分:5)
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(",")
对于这种用例,我会避免使用正则表达式。 urllib
模块更加明确,将处理URL编码的所有方面,并且都经过了良好的测试。
另一个出色的用于处理URL的第三方模块是出色的YARL。
答案 1 :(得分:2)
在带有元组包装的字符串上使用简单的re.search
:
lattitude, longitude = re.search(r'center=(.*?)%2C(.*?)&', s).groups()
其中s
是您的字符串(链接)。
示例:
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
答案 2 :(得分:1)
我猜想这个表达式可能返回我们想要的输出:
center=(-?\d+\.\d+)%2C(-?\d+\.\d+)
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
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)))
在this demo的右上角对表达式进行了说明,如果您想探索/简化/修改它,在this link中,您可以观察它如何与某些示例输入步骤匹配一步一步,如果您喜欢。