Python 2.7 Unicode GPS坐标到十进制的转换

Python 2.7 Unicode GPS坐标到十进制的转换,python-2.7,unicode,gps,Python 2.7,Unicode,Gps,问题 我使用的是python-2.7中的一段代码,生成的latlon表达式是一种无用的unicode格式(从调试器复制) 我想要这些作为浮子,也就是说 'latitude' = {float} 40.583044 'longitude' = {float} -74.741792 我尝试过的 我已将Unicode转换为字符串,例如: s = latitude.encode('utf-8') t = longitude.encode('utf-8') s = {str} 'N40°34\\'58.

问题

我使用的是python-2.7中的一段代码,生成的latlon表达式是一种无用的unicode格式(从调试器复制)

我想要这些作为浮子,也就是说

'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)])
这应该起作用:

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)])