I have a text like this:
**********************************************************************
********* *********
********* 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 *********
********* *********
**********************************************************************
I would like to write a function like:
def getKey(src,pattern,key):
...
where:
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
the result should ideally be a list of values of the key in the src
for instance:
Key=Key1 => [value1]
Key=Key2 => [Thu, 06.02.2020 22:28:22]
Key=Key4 => [dc960e6, ttt: 35044b5]
basically the value after the: while removing all the stars, spaces, ...
I do not know how to create the regex pattern for this
tried something like:
pattern = r'(?<=^*********).+(?=\*********$)'
Hope someone can help
You can use python-textops3 that is able to do many string manipulations/parsing:
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+\*+$'))
gives:
{'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'}
But if you want to get one special kay-value like you asked: your function will be:
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
So you could sub out the starts and ends with a regex like (where s
is the whole string):
re.sub(r'^[\*\s]+(.*?)[\*\s]+$', r'\1', s, flags=re.MULTILINE)
to get rid for the asterisks and spaces on the ends of the line. After this you can split each line with:
re.split(r':\s+', line, 1)
This will fail for lines with out :
, which you can catch. So something like:
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
Gives a d
with:
{'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']}
It can be parsed with an one-liner, no regexp. Input variable is 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 == ':'}
The same code formatted sanely:
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 dict:
{'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']}
Convert the value returned by the re.findall
function to a 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']}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.