Skip to content
Advertisement

Python, convert coordinate degrees to decimal points

I have a list of latitude and longitude as follows:

['33.0595° N', '101.0528° W']

I need to convert it to floats [33.0595, -101.0528].

Sure, the ‘-‘ is the only difference, but it changes when changing hemispheres, which is why a library would be ideal, but I can’t find one.

Advertisement

Answer

You can wrap the following code in a function and use it:

import re

l = ['33.0595° N', '101.0528° W']
new_l = []
for e in l:
    num = re.findall("d+.d+", e)
    if e[-1] in ["W", "S"]:
        new_l.append(-1. * float(num[0]))
    else:
        new_l.append(float(num[0]))

print(new_l)  # [33.0595, -101.0528]

The result match what you expect.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement