Answer the question
In order to leave comments, you need to log in
How to write parser for txt to dictionary using regex python?
Write a parser for a file with this content:
{## bonus:DS ##}
{## bang:fm,bv ##}
{## zf:123,230 ##} # note
{## zf1:349,410 ##}
{ ## pred:WE ##}
sadf sad ghjsgydk as
{## grable:Barash-prototepin.f31.txt ##}
{## grable:Barash-vkk yyy.s34.swt; ##}
rtsaydi ioi adioidsou asdy
{## da_vate: ##}
{# pronpun #}
{## te_zagol: + ##}
{## te_zagol2 : +##}
{## te_opisanie : b ##}
{## te_podtver_vushe_pricr :+ ##}
{## te_golde_babck : ##}
output dictionary 'grandle' is a list, 'zf' is a list with a tuple, all other pairs (key-value):
{ 'grable': ['Barash-prototepin.f31.txt', 'grable:Barash-vkk yyy.s34.swt'], # this is the list
'bonus': 'DS',
'te_podtver_vushe_pricr': '+',
' te_golde_babck': '',
'te_opisanie': 'b',
'da_vate': '',
'te_zagol': '+',
'te_zagol2': '+',
'bang': 'fm,bv',
'zf' : {('zf', '123,230'), ('zf1': '349, 410')], # list of tuples
all I got:
import re
def get_template_vars(filename):
result = {}
with open( filename, encoding='UTF-8') as lines:
for line in lines:
f = re.search(r,line)
result[f]
print (result)
python have been studying for a couple of days..
Thank you for your help.
Answer the question
In order to leave comments, you need to log in
1. Let's say we want to catch everything that is between the characters {## and ##}. What will the regular expression look like?
We don't care what characters will be found inside, so we can put a period - an arbitrary character. Let this symbol be repeated an arbitrary number of times - add an asterisk.
We need to catch everything that is between {## and ## } - we add parentheses to indicate the boundaries of the group: occur at least once (an asterisk means that it may occur zero or more times).
You can apply it to each line of the file in turn. And you can apply to the entire file if it is small.
Let's say we read the entire file into a single string variable:
with open('input.txt', 'r') as f:
text = f.read()
import re
pattern = "{## (.*) ##}"
result = re.findall(pattern, text)
['bonus:DS',
'bang:fm,bv',
'zf:123,230',
'zf1:349,410',
... ]
s = 'bonus:DS'
s.split(':') # получаем ['bonus', 'DS']
[('bonus', 'DS'),
('bang', 'fm,bv'),
('zf', '123,230'),
('zf1', '349,410'),
... ]
list = []
list.append('a')
d = dict()
d['a'] = '123'
x = '123'
y = '456'
tuple = (x, y)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question