感谢使用单行成语帮助有效地执行以下操作。
我有一个由大括号分隔的组的字符串,如下所示:
{1:xxxx}{2:xxxx}{3:{10:xxxx}}{4:xxxx\r\n:xxxx}....
如何将其转换为字典格式?
dict={1:'xxx',2:'xxxx',3:'{10:xxxx}'},4:'xxxx\r\n:xxxx'}
r = """(?x)
{
(\w+)
:
(
(?:
[^{}]
|
{.+?}
)+
)
}
"""
z = "{1:xxxx}{2:xxxx}{3:{10:xxxx}}{4:'xxxx'}"
print dict(re.findall(r, z))
# {'1': 'xxxx', '3': '{10:xxxx}', '2': 'xxxx', '4': "'xxxx'"}
如果需要,请随意转换为单行 - 只需(?x)
从正则表达式中删除所有空格。
上面只解析了一层嵌套,为了处理任意深度,考虑支持递归模式的更高级的正则表达式模块:
import regex
r = """(?x)
{
(\w+)
:
(
(?:
[^{}]
|
(?R)
)+
)
}
"""
z = "{1:abc}{2:{3:{4:foo}}}{5:bar}"
print dict(regex.findall(r, z))
# {'1': 'abc', '2': '{3:{4:foo}}', '5': 'bar'}
我会这样做:
raw = """{1:xxxx}{2:xxxx}{3:{10:xxxx}}{4:'xxxx\r\n:xxxx'}"""
def parse(raw):
# split into chunks by '}{' and remove the outer '{}'
parts = raw[1:-1].split('}{')
for part in parts:
# split by the first ':'
num, data = part.split(':', 1)
# yield each entry found
yield int(num), data
# make a dict from it
print dict(parse(raw))
'{10:xxxx}'
就像在您的示例中一样,它保留为字符串。