Unicode GPS 坐标到十进制的转换

Unicode GPS Coordinates to Decimal Conversion

问题

我正在使用 python-2.7 中的代码,并且我得到的 latlon 表达式采用无用的 unicode 格式(从调试器复制)

'latitude' = {unicode} u'N40°34\'58.96"'
'longitude' = {unicode} u'W074°44\'30.45"'

我想要这些作为浮点数,即

'latitude' = {float} 40.583044
'longitude' = {float} -74.741792

我试过的

我已将 unicode 转换为字符串,例如:

s = latitude.encode('utf-8')
t = longitude.encode('utf-8')
s = {str} 'N40°34\'58.96"'
t = {str} 'W074°44\'30.45"'

现在的问题是我需要在转换为正确的十进制表达式之前删除所有不需要的字符

这应该有效:

import re
def parseLonLat(l):
    l = re.split('[^\d\w\.]+', l)[:-1] # split into a list degree, minute, and second
    direction = l[0][0] # direction North, East, South or West
    l[0] = l[0][1:] # remove the character N or E or S or W
    return (1 if direction in ('N', 'E') else -1) * sum([float(n) / 60 ** i for i, n in enumerate(l)])