从包含键值对的字符串中获取python字典 [英] get python dictionary from string containing key value pairs

查看:878
本文介绍了从包含键值对的字符串中获取python字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

推荐答案

>>> 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'}

说明:

\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

这篇关于从包含键值对的字符串中获取python字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆