How to convert degree minute second to degree decimal
Here's my one liner (fine, fine – maybe it's two lines) :)
import re
lat = '''51°36'9.18"N'''
deg, minutes, seconds, direction = re.split('[°\'"]', lat)
(float(deg) + float(minutes)/60 + float(seconds)/(60*60)) * (-1 if direction in ['W', 'S'] else 1)
This outputs 51.60255
The problem is that the seconds 44.29458 are split at .
.
You could either define the split characters directly (instead of where not to split):
>>> re.split('[°\'"]+', """78°55'44.29458"N""")
['78', '55', '44.29458', 'N']
or leave the regular expression as it is and merge parts 2 and 3:
dms2dd(parts[0], parts[1], parts[2] + "." + parts[3], parts[4])
Update:
Your method call dd = parse_dms("78°55'44.33324"N )
is a syntax error. Add the closing "
and escape the other one. Or use tripple quotes for the string definition:
parse_dms("""78°55'44.29458"N""")
The function above (dms2dd) is incorrect.
Actual (With error):
if direction == 'E' or direction == 'N': dd *= -1
Corrected Condition:
if direction == 'W' or direction == 'S': dd *= -1