簡體   English   中英

需要正則表達式從python字典中提取字符

[英]Need regex to extract characters from python dictionary

我有一本字典,從那里可以打印出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')

我要將這些數字轉換為十進制度。 例如,

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

典型的python字符拆分無法正常工作,因為數字的模式不一致。

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

我使用此線程從docx文件中提取了這些值。 現在,我再次陷入困境。

您可以參加(請參閱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