I have a python string in the format:
str = "name: srek age :24 description: blah blah"
is there any way to convert it to a dictionary that looks like
{'name': 'srek', 'age': '24', 'description': 'blah blah'}
where each record is a pair (key, value) taken from a string. I tried breaking a string into a list
str.split()
and then manually delete :
by checking each tag name, adding to the dictionary. The disadvantage of this method is: this method is unpleasant, I need to manually delete :
for each pair, and if there are several words "value" in the line (for example, blah blah
for description
), each word will be a separate entry in the list, which is undesirable. Is there any Pythonic way to get a dictionary (using python 2.7)?
source share