简体   繁体   English

需要正则表达式从python字典中提取字符

[英]Need regex to extract characters from python dictionary

I have a dictionary from where I could print out the data['longitude'] and data['latitude'] like this. 我有一本字典,从那里可以打印出data['longitude']data['latitude']这样的data['latitude']

(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. 典型的python字符拆分无法正常工作,因为数字的模式不一致。

(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. 我使用此线程从docx文件中提取了这些值。 Now I am stuck again. 现在,我再次陷入困境。

You could go for (see a demo on regex101.com ): 您可以参加(请参阅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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM