如何将这些输出坐标转换为标准外观坐标?
how can I convert these outputted coordinates to standard looking ones?
我有这段输出端口坐标的代码:
import urllib
import urllib.request as request
import re
a = input("What country is your port in?: ")
b = input("What is the name of the port?: ")
url = "http://ports.com/"
country = ["united-kingdom","greece"]
ports = ["port-of-eleusis","portsmouth-continental-ferry-port","poole-harbour"]
totalurl = "http://ports.com/" + a + "/" + b + "/"
htmlfile = urllib.request.urlopen(totalurl)
htmltext = htmlfile.read()
regex = '<strong>Coordinates:</strong>(.*?)</span>'
pattern = re.compile(regex)
with urllib.request.urlopen(totalurl) as response:
html = htmltext.decode()
num = re.findall(pattern, html)
print(num)
输出正确且可读,但我需要这样格式的坐标:39°09'24.6''N 175°37'55.8''W 而不是:
>>> [' 50°48′41.04″N 1°5′31.31″W']
您的错误是因为 HTML 在内部使用这些代码来显示特定的 unicode 字符,而 python 没有。要解决此问题,请将 print(num)
替换为 print(list(i.replace('°', "°").replace('′',"′").replace('″',"″") for i in num))
这基本上将 °
替换为 °
,将 ′
替换为 ′
,将 ″
替换为 ″
。
>>> print(list(i.replace('°', "°").replace('′',"′").replace('″',"″") for i in num))
[" 50°48′41.04″N 1°5′31.31″W"]
>>>
我有这段输出端口坐标的代码:
import urllib
import urllib.request as request
import re
a = input("What country is your port in?: ")
b = input("What is the name of the port?: ")
url = "http://ports.com/"
country = ["united-kingdom","greece"]
ports = ["port-of-eleusis","portsmouth-continental-ferry-port","poole-harbour"]
totalurl = "http://ports.com/" + a + "/" + b + "/"
htmlfile = urllib.request.urlopen(totalurl)
htmltext = htmlfile.read()
regex = '<strong>Coordinates:</strong>(.*?)</span>'
pattern = re.compile(regex)
with urllib.request.urlopen(totalurl) as response:
html = htmltext.decode()
num = re.findall(pattern, html)
print(num)
输出正确且可读,但我需要这样格式的坐标:39°09'24.6''N 175°37'55.8''W 而不是:
>>> [' 50°48′41.04″N 1°5′31.31″W']
您的错误是因为 HTML 在内部使用这些代码来显示特定的 unicode 字符,而 python 没有。要解决此问题,请将 print(num)
替换为 print(list(i.replace('°', "°").replace('′',"′").replace('″',"″") for i in num))
这基本上将 °
替换为 °
,将 ′
替换为 ′
,将 ″
替换为 ″
。
>>> print(list(i.replace('°', "°").replace('′',"′").replace('″',"″") for i in num))
[" 50°48′41.04″N 1°5′31.31″W"]
>>>