如何将这些输出坐标转换为标准坐标?

时间:2017-11-28 23:06:25

标签: python-3.x converter implicit-conversion coordinate-systems

我有这个输出端口坐标的代码:

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&deg;48&prime;41.04&Prime;N 1&deg;5&prime;31.31&Prime;W']

1 个答案:

答案 0 :(得分:0)

您的错误是由于HTML内部使用这些代码来显示特定的unicode字符,而python则没有。要解决此问题,请将print(num)替换为print(list(i.replace('&deg;', "°").replace('&prime;',"′").replace('&Prime;',"″") for i in num))

这基本上将&deg;替换为°&prime;替换为&Prime;替换为

>>> print(list(i.replace('&deg;', "°").replace('&prime;',"′").replace('&Prime;',"″") for i in num))
[" 50°48′41.04″N 1°5′31.31″W"]
>>> 
相关问题