i have a python string in the format:
str = "name: srek age :24 description: blah blah"
is there any way to convert it to dictionary that looks like
{'name': 'srek', 'age': '24', 'description': 'blah blah'}
where each entries are (key,value) pairs taken from string. I tried splitting the string to list by
str.split()
and then manually removing :
, checking each tag name, adding to a dictionary. The drawback of this method is: this method is nasty, I have to manually remove :
for each pair and if there is multi word 'value' in string (for example, blah blah
for description
), each word will be a separate entry in a list which is not desirable. Is there any Pythonic way of getting the dictionary (using python 2.7) ?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…