P
P
Pyty2014-03-06 22:37:48
Python
Pyty, 2014-03-06 22:37:48

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

3 answer(s)
V
Valentine, 2014-03-07
@vvpoloskin

Do you want people here to do your work for you?

T
traims, 2014-03-18
@traims

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()

Then we can pull out everything that interests us at once:
import re
pattern = "{## (.*) ##}"
result = re.findall(pattern, text)

The result will be a list of strings like
['bonus:DS',
'bang:fm,bv',
'zf:123,230',
'zf1:349,410',
... ]

Here each line can be split into two:
s = 'bonus:DS'
s.split(':')  # получаем ['bonus', 'DS']

- and proceed as needed.
To avoid using split(), we can initially include a colon in our regular expression:
The result is a list of tuples:
[('bonus', 'DS'),
('bang', 'fm,bv'),
('zf', '123,230'),
('zf1', '349,410'),
... ]

Examples of how lists, dictionaries and tuples can be built from scratch. All examples are easy to check on the command line.
Lists:
list = []
list.append('a')

Key-value pairs (dictionaries):
d = dict()
d['a'] = '123'

Tuples:
x = '123'
y = '456'
tuple = (x, y)

In theory, this information should be enough for you to figure out how to solve the problem. Good luck!

P
Pyty, 2014-03-07
@Pyty

I want people to give advice on how to do this, a person who has no experience, maybe help find a solution or at least point me in the right direction.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question