python解析以星号开头和结尾的文本

2024-04-20 04:23:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这样一个文本:

**********************************************************************
*********                                                    *********
*********     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                                    *********
*********                                                    *********
**********************************************************************

我想写一个函数,比如:

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'(?<=^*********).+(?=\*********$)'

希望有人能帮忙


Tags: thekey文本srcseppatternkey2key1
3条回答

它可以用一行程序解析,不需要regexp。输入变量为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 == ':'
    }

输出指令:

{'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']}

因此,您可以使用类似正则表达式的子表达式(其中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']}

您可以使用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值,就像你问的:你的函数是:

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

相关问题 更多 >