[英]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.