Форум сайта python.su
0
Привет!
имеется строки вида:
“Location: Some Location Address: Some, Interesting, Adress Phone: 543-232-32-32 Fax:”
“Location: Some Location Address: , , Adress Phone: Fax:”
Как это распарсить в словарик вида:
{'Location“: ”Some Location“, ”Address“: ”Some, Interesting, Adress“, ”Phone“: ”543-232-32-32“, ”Fax“:”“}
{'Location”: “Some Location”, “Address”: “, , Adress”, “Phone”: “”, “Fax”:“”}
Т/е/ ключи в строке есть всегда но значения в них не всегда присутствуют
Спасибо!
Офлайн
76
Может так?
import re st = 'Location: Some Location Address: , , Adress Phone: Fax:' print(dict(zip(re.findall(r'\w+:',st), re.split(r'\w+:',st)[1:])))
Офлайн
0
спасибо большое! работает как надо
Офлайн