2015-09-04 40 views
2

讓我們有這樣的文字:以不同的順序檢索多個捕獲組?

2343 abcd 
ajek 4827 
1231 skj0 

而這正則表達式:

/(?:(?P<number>\d{4})\s+(?P<text>\w{4})|(?P<text>\w{4})\s+(?P<number>\d{4}))/Jg 

DEMO:https://regex101.com/r/yN9zK4/1

,其目的是獲取這樣的:

{{ number: 2343, text: abcd }, 
{ number: 4827, text: ajek }, 
{ number: 1231, text: skj0 }} 

隨着我有沒有問題得到我的正則表達式的工作。

看來Python不提供J選項。如何使用除正則表達式之外的其他機制使其與Python協同工作?

+0

,我不認爲這是使用Python re或甚至regex包可以爲不同的捕獲組使用相同的名稱。 – nhahtdh

+0

好的,我應該更新我的問題,然後 – nowox

+0

除了正則表達式,我想你可以使用更通用的正則表達式(兩個標記'\ W +')來匹配字符串並提取2個標記,然後做另一個測試來對它們進行分類。 – nhahtdh

回答

2

僅通過拆分。這將產生字典列表。

import re 
from collections import OrderedDict 

with open(r'file') as f: 
    h = OrderedDict() 
    l = [] 
    for line in f: 
     d = {} 
     f = line.split() 
     for i in f: 
      if i.isdigit() and len(i) == 4: 
       d['number'] = i 
      else: 
       d['text'] = i 
     l.append(d) 

    print(l)  
+1

針對13行Python的1行Perl。你讓我今天一整天都感覺很好 :( – nowox

1

在你的問題中,你寫出輸出結構{{a:1},{b:2},{c:3}}。這種結構是不可能的,因爲你不能在python中創建一組字典。集合只能包含未更改的對象,如數字,字符串或元組。

你可以使用列表理解和簡單的方法和功能解析這個文本到字典結構:

>>> multiline = '''2343 abcd 
... ajek 4827 
... 1231 skj0''' 
... 
>>> [{'number':y[0],'text':y[1]} for y in (sorted(x.split()) for x in multiline.split('\n'))] 
[{'text': 'abcd', 'number': '2343'}, {'text': 'ajek', 'number': '4827'}, {'text': 'skj0', 'number': '1231'}] 
>>> 

或者使用線性詞典發電機,像這樣:

>>> {int(k):v for k,v in [sorted(x.split()) for x in multiline.split('\n')]} 
{4827: 'ajek', 1231: 'skj0', 2343: 'abcd'}