I have a string which looks something like
text = "customer: Anna Smith; payment: 123; date: 12-02-2020; customer: Jack; payment: 10.3; date: 20-03-2020"
Now I want to turn it into a list of tuples (which later I can use to create a dictionary):
[('customer', 'Anna Smith'),
('payment', '123'),
('date', '12-02-2020'),
('customer', 'Jack'),
('payment', '10.3'),
('date', '20-03-2020')]
I tried to use re.findall for this purpose in the following way:
re.findall(u'(\w+): (.+?);', text)
Of course it doesn't capture the last pair of a key and a value, because of the semicolon in the regular expression. I think that I need an if-else if operation here: if the parser encounters a semicolon, then it extracts the words, else it checks for a regular expression for EOL (\Z
). Please, help