[英]python parse text that starts and ends with asterisks
我有這樣的文字:
**********************************************************************
********* *********
********* llgalfaslfjasljflksjaglajlgjlajlgfaslg *********
********* *********
********* Key1: value1 *********
********* *********
********* Key2: Thu, 06.02.2020 22:28:22 *********
********* *********
********* Key3: Sep 30 2019-02:35:32 *********
********* key4: dc960e6, ttt: 35044b5 *********
********* *********
********* Key5: Value5 *********
********* *********
********* Key6: Value6 *********
********* *********
**********************************************************************
我想寫一個 function 像:
def getKey(src,pattern,key):
...
在哪里:
source = the text as seen in the sample above (can be more or les )
pattern = regex pattern
key = the key I would like to get the value of
理想情況下,結果應該是 src 中鍵的值列表
例如:
Key=Key1 => [value1]
Key=Key2 => [Thu, 06.02.2020 22:28:22]
Key=Key4 => [dc960e6, ttt: 35044b5]
基本上是后面的值:同時刪除所有的星星,空格,......
我不知道如何為此創建正則表達式模式
嘗試過類似的東西:
pattern = r'(?<=^*********).+(?=\*********$)'
希望有人能幫忙
您可以使用能夠進行許多字符串操作/解析的 python-textops3:
from textops import *
s = '''
**********************************************************************
********* *********
********* llgalfaslfjasljflksjaglajlgjlajlgfaslg *********
********* *********
********* Key1: value1 *********
********* *********
********* Key2: Thu, 06.02.2020 22:28:22 *********
********* *********
********* Key3: Sep 30 2019-02:35:32 *********
********* key4: dc960e6, ttt: 35044b5 *********
********* *********
********* Key5: Value5 *********
********* *********
********* Key6: Value6 *********
********* *********
**********************************************************************
'''
print(s | keyval(r'^\*+\s*(?P<key>\w+)\s*:\s*(?P<val>.+?)\s+\*+$'))
給出:
{'key1': 'value1', 'key2': 'Thu, 06.02.2020 22:28:22', 'key3': 'Sep 30 2019-02:35:32', 'key4': 'dc960e6, ttt: 35044b5', 'key5': 'Value5', 'key6': 'Value6'}
但是,如果您想像您所問的那樣獲得一個特殊的 kay 值:您的 function 將是:
def getKey(src,pattern,key):
return src | find_pattern(pattern.format(key=key))
pattern = r'^\*+\s*{key}\s*:\s*(.+?)\s+\*+$'
print(getKey(s,pattern,'Key1'))
value1
因此,您可以使用正則表達式(其中s
是整個字符串)來分出開頭和結尾:
re.sub(r'^[\*\s]+(.*?)[\*\s]+$', r'\1', s, flags=re.MULTILINE)
去掉行尾的星號和空格。 在此之后,您可以使用以下命令拆分每一行:
re.split(r':\s+', line, 1)
對於沒有 out :
的行,這將失敗,您可以捕獲它。 所以像:
import re
lines = re.sub(r'^[\*\s]+(.*?)[\*\s]+$', r'\1', s, flags=re.MULTILINE).split('\n')
d = {}
for line in lines:
try:
key, value = re.split(r':\s+', line, 1)
except ValueError:
continue
value = value.split(',')
d[key] = value
給出一個d
:
{'Key1': ['value1'],
'Key2': ['Thu', ' 06.02.2020 22:28:22'],
'Key3': ['Sep 30 2019-02:35:32'],
'key4': ['dc960e6', ' ttt: 35044b5'],
'Key5': ['Value5'],
'Key6': ['Value6']}
它可以用單行解析,沒有正則表達式。 輸入變量是text
。
dct = {key: [v.strip() for v in value.split(',')] for key, sep, value in (line.strip(' *').partition(':') for line in text.splitlines()) if sep == ':'}
相同的代碼格式正確:
dct = {
key: [v.strip() for v in value.split(',')]
for key, sep, value in (
line.strip(' *').partition(':')
for line in text.splitlines())
if sep == ':'
}
Output 字典:
{'Key1': ['value1'], 'Key2': ['Thu', '06.02.2020 22:28:22'], 'Key3': ['Sep 30 2019-02:35:32'], 'key4': ['dc960e6', 'ttt: 35044b5'], 'Key5': ['Value5'], 'Key6': ['Value6']}
將re.findall
function 返回的值轉換為dict
object:
import re
from pprint import pprint
r = r'^\*+\s*(\w+)\s*:\s*(.+?)\s+\*+$'
txt = '''
**********************************************************************
********* *********
********* llgalfaslfjasljflksjaglajlgjlajlgfaslg *********
********* *********
********* Key1: value1 *********
********* *********
********* Key2: Thu, 06.02.2020 22:28:22 *********
********* *********
********* Key3: Sep 30 2019-02:35:32 *********
********* key4: dc960e6, ttt: 35044b5 *********
********* *********
********* Key5: Value5 *********
********* *********
********* Key6: Value6 *********
********* *********
**********************************************************************
'''
res = {k: [v.strip() for v in vv.split(',')] for k, vv in re.findall(r, txt, re.M)}
pprint(res)
Output:
{'Key1': ['value1'],
'Key2': ['Thu', '06.02.2020 22:28:22'],
'Key3': ['Sep 30 2019-02:35:32'],
'Key5': ['Value5'],
'Key6': ['Value6'],
'key4': ['dc960e6', 'ttt: 35044b5']}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.