将字符串的部分解析到字典中?

2024-04-23 15:04:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我不熟悉字符串解析库;我想从:

'foo=5 z v xz er bar=" hel o" c z a == "hi" b = "who"'

此已解析词典:

{'foo':5, 'bar': ' hel o', 'a': 'hi', b: 'who'}

但我不知道从哪里开始。你能给我一些处理这个转换的建议吗?你知道吗


Tags: 字符串foobarhi建议词典erxz
2条回答

你可以用正则表达式。见python's documentation on regextutorial's point tutorial。你知道吗

像这样的方法可以奏效:

import re

regex = re.compile(r"(\w+ ?=+ ?\d+|\w+ ?=+ ?\"(?: *\w*)*\")")

#your example string:
s = 'foo=5 z v xz er bar=" hel o" c z a == "hi" b = "who"'

matches = regex.findall(s)

dict1 = {}
for m in matches:
    elems = m.split("=")
    #elems[0] = key
    #elems[len(elems)-1] = value, to account for the case of multiple ='s

    try:
        #see if the element is a number
        dict1[str(elems[0])] = int(elems[len(elems) - 1]) 

    except:
        #if type casting didn't work, just store it as a string
        dict1[str(elems[0])] = elems[len(elems) - 1] 

下面是正则表达式的分解:

(\w+ ?=+ ?\d+|\w+ ?=+ ?\"(?: *\w*)*\")

\w+表示一个或多个字母数字字符。你知道吗

\d+表示一个或多个数字。你知道吗

(?:regex)*表示匹配0个或多个regex副本,而不为其分配组#。你知道吗

(regex1|regex2)表示查找与regex1或regex2匹配的字符串。你知道吗

\"是引号的转义序列。你知道吗

=+表示匹配一个或多个“=”符号

_?表示匹配0或1个空格(假设“304;”是一个空格)

Pyparsing是一个解析库,它允许您一次构建一点匹配的表达式。你知道吗

from pyparsing import Word, alphas, alphanums, nums, oneOf, quotedString, removeQuotes

identifier = Word(alphas, alphanums)
integer = Word(nums).setParseAction(lambda t: int(t[0]))
value = integer | quotedString.setParseAction(removeQuotes)

# equals could be '==' or '='
# (suppress it so it does not get included in the resulting tokens)
EQ = oneOf("= ==").suppress()

# define the expression for an assignment
assign = identifier + EQ + value

下面是应用此解析器的代码

# search sample string for matching assignments
s = 'foo=5 z v xz er bar=" hel o" c z a == "hi" b = "who"'
assignments = assign.searchString(s)
dd = {}
for k,v in assignments:
    dd[k] = v

# or more simply
#dd = dict(assignments.asList())

print dd

提供:

{'a': 'hi', 'b': 'who', 'foo': 5, 'bar': ' hel o'}

相关问题 更多 >