I have a list of latitude and longitude as follows:
JavaScript
x
2
1
['33.0595° N', '101.0528° W']
2
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:
JavaScript
1
13
13
1
import re
2
3
l = ['33.0595° N', '101.0528° W']
4
new_l = []
5
for e in l:
6
num = re.findall("d+.d+", e)
7
if e[-1] in ["W", "S"]:
8
new_l.append(-1. * float(num[0]))
9
else:
10
new_l.append(float(num[0]))
11
12
print(new_l) # [33.0595, -101.0528]
13
The result match what you expect.