0

I have a dictionary from where I could print out the data['longitude'] and data['latitude'] like this.

(91°38'28.2"E)(22°40'34.3"N)
(92°04´14.1´´E)(21°37´00.8´´N)
(E-092° 15. 715')(N-20° 56.062')
(91°49'10.63"E)(24°20'05.40"N)
(91°26'31.92"E)(24°07'35.15"N)
(90°08'15.07"E)(24°41'14.71"N)
(90°04'7.97"E)(24°42'29.34"N)
(90°04'10.06"E)(24°42'32.8"N)
(E-092° 15.776')(N-20° 56.065')
(91°46'26.90"E)(24°18'47.16"N)
(E-092° 15.649')(N-20° 56.023')
(91°46'26.90"E)(24°18'47.16"N)
(91°49'08.08"E)(24°20'06.33"N)
(92° 2'31.25"E)(21°20'58.79"N)
(E-092° 15.776')(N-20° 56.065')
(E-092° 15. 486')(N-20° 56.022')

I am to convert these number to decimal degrees. For example,

92° 2'31.25"E -> (92 + (2/60) + (31.25/3600)) -> 92.042
20° 56.023' -> 20 + (56.023/60) -> 20.993

Typical python character split couldn't work because the numbers have inconsistent patterns.

(data['longitude'][:3]) + (data['longitude'][5:2]/60) + (data['longitude'][8:5]/3600) 

I used this thread to extract these values from a docx file. Now I am stuck again.

4
  • what is your input, a list of tuples or a list of strings? Commented Apr 22, 2017 at 15:17
  • Have you tried playing around in an online regex tester? Like regex101.com? Commented Apr 22, 2017 at 15:27
  • Unicode or ascii? Commented Apr 22, 2017 at 15:42
  • My inputs are unicode strings - @RomanPerekhrest. I am still working with the regex code. (N-(.{,12})([0-9]|\')|[0-9].{,12}N)[;, ]+(E-(.{,12})([0-9]|\')|[0-9].{,12}E). It needs to either search by 90-92 and 20-24 degree range first, or search the minute and seconds then come to degrees. Commented Apr 22, 2017 at 15:47

1 Answer 1

1

You could go for (see a demo on regex101.com):

import re

coordinates = """
(91°38'28.2"E)(22°40'34.3"N)
(92°04´14.1´´E)(21°37´00.8´´N)
(E-092° 15. 715')(N-20° 56.062')
(91°49'10.63"E)(24°20'05.40"N)
(91°26'31.92"E)(24°07'35.15"N)
(90°08'15.07"E)(24°41'14.71"N)
(90°04'7.97"E)(24°42'29.34"N)
(90°04'10.06"E)(24°42'32.8"N)
(E-092° 15.776')(N-20° 56.065')
(91°46'26.90"E)(24°18'47.16"N)
(E-092° 15.649')(N-20° 56.023')
(91°46'26.90"E)(24°18'47.16"N)
(91°49'08.08"E)(24°20'06.33"N)
(92° 2'31.25"E)(21°20'58.79"N)
(E-092° 15.776')(N-20° 56.065')
(E-092° 15. 486')(N-20° 56.022')
"""

rx = re.compile(r"(?P<degree>-?\d+)°\s*(?P<minute>[^'´]+)'")

def convert(match):
    try:
        degree = float(match.group('degree'))
        minute = float(match.group('degree'))
        result = degree + minute/60
    except:
        result = -1
    finally:
        return result

coordinates_new = [convert(match) for match in rx.finditer(coordinates)]
print(coordinates_new)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Jan. I ended up using this expression - ((?P<degree>\d+)°\s*(?P<minute>[^\'´]+)[\'´]?\b(?P<second>[^\"´´]+)). I needed to convert the seconds too. I think you need to change degree to minute inside minute = float(match.group('degree')).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.