Найти - Пользователи
Полная версия: Парсинг строки по слову с ":"
Начало » Python для новичков » Парсинг строки по слову с ":"
1
metotron
Привет!
имеется строки вида:
“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”:“”}
Т/е/ ключи в строке есть всегда но значения в них не всегда присутствуют
Спасибо!
Romissevd
Может так?
 import re
st = 'Location: Some Location Address: , , Adress Phone: Fax:'
print(dict(zip(re.findall(r'\w+:',st), re.split(r'\w+:',st)[1:])))
metotron
спасибо большое! работает как надо
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB