从包含键值对的字符串中获取python字典

时间:2022-01-01 20:57:31

i have a python string in the format:

我有一个格式的python字符串:

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) ?

然后手动删除:,检查每个标签名称,添加到字典中。这种方法的缺点是:这个方法很讨厌,我必须手动删除:对于每一对,如果字符串中有多个单词'value'(例如,blah blah for description),每个单词将是一个单独的条目一个不可取的清单。是否有任何Pythonic方式获取字典(使用python 2.7)?

3 个解决方案

#1


31  

>>> r = "name: srek age :24 description: blah blah"
>>> import re
>>> regex = re.compile(r"\b(\w+)\s*:\s*([^:]*)(?=\s+\w+\s*:|$)")
>>> d = dict(regex.findall(r))
>>> d
{'age': '24', 'name': 'srek', 'description': 'blah blah'}

Explanation:

说明:

\b           # Start at a word boundary
(\w+)        # Match and capture a single word (1+ alnum characters)
\s*:\s*      # Match a colon, optionally surrounded by whitespace
([^:]*)      # Match any number of non-colon characters
(?=          # Make sure that we stop when the following can be matched:
 \s+\w+\s*:  #  the next dictionary key
|            # or
 $           #  the end of the string
)            # End of lookahead

#2


2  

without re:

没有重新:

r = "name: srek age :24 description: blah blah cat: dog stack:overflow"
lis=r.split(':')
dic={}
try :
 for i,x in enumerate(reversed(lis)):
    i+=1
    slast=lis[-(i+1)]
    slast=slast.split()
    dic[slast[-1]]=x

    lis[-(i+1)]=" ".join(slast[:-1])
except IndexError:pass    
print(dic)

{'age': '24', 'description': 'blah blah', 'stack': 'overflow', 'name': 'srek', 'cat': 'dog'}

#3


0  

Other variation of Aswini program which display the dictionary in original order

Aswini程序的其他变体,以原始顺序显示字典

import os
import shutil
mystr = "name: srek age :24 description: blah blah cat: dog stack:overflow"
mlist = mystr.split(':')
dict = {}
list1 = []
list2 = []
try:
 for i,x in enumerate(reversed(mlist)):
    i = i + 1
    slast = mlist[-(i+1)]
    cut = slast.split()
    cut2 = cut[-1]
    list1.insert(i,cut2)
    list2.insert(i,x)
    dict.update({cut2:x})
    mlist[-(i+1)] = " ".join(cut[0:-1])
except:
 pass   

rlist1 = list1[::-1]
rlist2= list2[::-1]

print zip(rlist1, rlist2)

Output

产量

[('name', 'srek'), ('age', '24'), ('description', 'blah blah'), ('cat', 'dog'), ('stack', 'overflow')]

[('name','srek'),('age','24'),('description','blah blah'),('cat','dog'),('stack','overflow' )]

#1


31  

>>> r = "name: srek age :24 description: blah blah"
>>> import re
>>> regex = re.compile(r"\b(\w+)\s*:\s*([^:]*)(?=\s+\w+\s*:|$)")
>>> d = dict(regex.findall(r))
>>> d
{'age': '24', 'name': 'srek', 'description': 'blah blah'}

Explanation:

说明:

\b           # Start at a word boundary
(\w+)        # Match and capture a single word (1+ alnum characters)
\s*:\s*      # Match a colon, optionally surrounded by whitespace
([^:]*)      # Match any number of non-colon characters
(?=          # Make sure that we stop when the following can be matched:
 \s+\w+\s*:  #  the next dictionary key
|            # or
 $           #  the end of the string
)            # End of lookahead

#2


2  

without re:

没有重新:

r = "name: srek age :24 description: blah blah cat: dog stack:overflow"
lis=r.split(':')
dic={}
try :
 for i,x in enumerate(reversed(lis)):
    i+=1
    slast=lis[-(i+1)]
    slast=slast.split()
    dic[slast[-1]]=x

    lis[-(i+1)]=" ".join(slast[:-1])
except IndexError:pass    
print(dic)

{'age': '24', 'description': 'blah blah', 'stack': 'overflow', 'name': 'srek', 'cat': 'dog'}

#3


0  

Other variation of Aswini program which display the dictionary in original order

Aswini程序的其他变体,以原始顺序显示字典

import os
import shutil
mystr = "name: srek age :24 description: blah blah cat: dog stack:overflow"
mlist = mystr.split(':')
dict = {}
list1 = []
list2 = []
try:
 for i,x in enumerate(reversed(mlist)):
    i = i + 1
    slast = mlist[-(i+1)]
    cut = slast.split()
    cut2 = cut[-1]
    list1.insert(i,cut2)
    list2.insert(i,x)
    dict.update({cut2:x})
    mlist[-(i+1)] = " ".join(cut[0:-1])
except:
 pass   

rlist1 = list1[::-1]
rlist2= list2[::-1]

print zip(rlist1, rlist2)

Output

产量

[('name', 'srek'), ('age', '24'), ('description', 'blah blah'), ('cat', 'dog'), ('stack', 'overflow')]

[('name','srek'),('age','24'),('description','blah blah'),('cat','dog'),('stack','overflow' )]