gpt4 book ai didi

python - Pyparsing:使用键=值解析嵌套明文数据

转载 作者:太空宇宙 更新时间:2023-11-04 04:39:10 25 4
gpt4 key购买 nike

我是 python 的新手,我正在尝试使用看起来像这样的 pyparsing 来解析一些数据

string2 = """
object1 {
key1 = value1
key2 = value2
#key3 = value3
key4 = value4
#key5 = value5
key6 = value6
subobject1 {
key1 = value1
key2 = value2
key3 = value3
}
}
"""

我可以使用这段代码得到一个键=值对

def parse_objects(source):
LBRACE,EQ,RBRACE,HASH = map(Suppress, '{=}#')
object_name = Word(printables)
#disable = MatchFirst(map(Literal, '#'.split()))
key = Word(printables)
value = Word(printables)

if LineStart() == HASH:
key_and_value = Group(HASH + key('key') + EQ + value('value'))
else:
key_and_value = Group(key('key') + EQ + value('value'))

collection = Forward()
object_body = Group(LBRACE + ZeroOrMore(collection | key_and_value) + RBRACE)
collection <<= Group(object_name + object_body)

return collection.parseString(source)

collection = parse_objects(string2)
print(collection.dump())

但是我还需要解析对象中不包含值,只包含键的数据。例如

object1 {
key1 = value1
key2
#key3 = value3
key4
#key5 = value5
key6 = value6
subobject1 {
key1 = value1
key2 = value2
key3 = value3
}
}

我尝试更改代码并添加检查表达式if value is None。像这样

if value is None:
key_and_value = Group(key('key'))
else:
if LineStart() == HASH:
key_and_value = Group(HASH + key('key') + EQ + value('value'))
else:
key_and_value = Group(key('key') + EQ + value('value'))

但是我得到一个错误

Match W:(0123...) at loc 19(3,9)
Matched W:(0123...) -> ['key1']
Match W:(0123...) at loc 25(3,15)
Matched W:(0123...) -> ['value1']
Match W:(0123...) at loc 41(4,9)
Matched W:(0123...) -> ['key2']
Traceback (most recent call last):
File "c:\Python27\my_projects\test_parser.py", line 86, in <module>
collection = parse_objects(string2)
File "c:\Python27\my_projects\test_parser.py", line 84, in parse_objects
return collection.parseString(source)
File "C:\Python27\lib\site-packages\pyparsing.py", line 1632, in parseString
raise exc
ParseException: Expected "}" (at char 41), (line:4, col:9)

我认为pyparsing把key作为子对象,并没有找到{。谁能给我任何建议?也许我需要改变我的语法方法?感谢您的帮助。

编辑 1

@Jappy 的解决方案非常适合我上面写的数据,当 subobject1 部分位于主要部分的底部时。在分析我的数据后,我发现在 subobject1 部分之后可能有更多的 key=value 对或只有键,像这样:

string2 = """
object1 {
key1 = value1
key2
#key3 = value3
key4 = value4
subobject1 {
key1 = value1
key2 = value2
key3 = value3
}
#key5 = value5
key6 = v_a_l_u_e_6
subobject2 {
key1 = value1
}
key7 = value7
key8
}
"""

输出如下:

[['object1', ['key1', 'value1'], ['key2', 'null'], ['#key3', 'value3'], ['key4', 'value4'], ['subobject1', ['key1', 'value1'], ['key2', 'value2'], ['key3', 'value3']], ['#key5', 'value5'], ['key6', 'v_a_l_u_e_6'], ['subobject2', ['key1', 'value1']], ['key7', 'value7'], ['key8', 'null']]]
- objects: ['object1', ['key1', 'value1'], ['key2', 'null'], ['#key3', 'value3'],
['key4', 'value4'], ['subobject1', ['key1', 'value1'], ['key2', 'value2'], ['key3', 'value3']], ['#key5', 'value5'], ['key6', 'v_a_l_u_e_6'], ['subobject2', ['key1', 'value1']], ['key7', 'value7'], ['key8', 'null']]
- key_val_lines: [['key7', 'value7'], ['key8', 'null']]
[0]:
['key7', 'value7']
- key: 'key7'
- val: 'value7'
[1]:
['key8', 'null']
- key: 'key8'
- val: 'null'
- obj_name: 'object1'
- objects: ['subobject2', ['key1', 'value1']]
- key_val_lines: [['key1', 'value1']]
[0]:
['key1', 'value1']
- key: 'key1'
- val: 'value1'
- obj_name: 'subobject2'

我把代码改成这样:

ParserElement.inlineLiteralsUsing(Suppress)
name_expr = Word(printables, excludeChars='{}')
key_val_expr = '=' + Word(printables)

key_val_line = Group(name_expr('key') + (lineEnd().setParseAction(lambda t: 'null') | key_val_expr)('val'))
#key_val_lines = OneOrMore(key_val_line)('key_val_lines')

obj = Forward()
objects = Group('{' + OneOrMore(key_val_line | obj) + '}')
obj << Group(name_expr('obj_name') + objects('objects'))
#obj << Group(name_expr('obj_name') + '{' + OneOrMore(key_val_lines | obj) + '}')('objects')

o = obj.parseString(string2)
print o.dump()

结果是:

[['object1', [['key1', 'value1'], ['key2', 'null'], ['#key3', 'value3'], ['key4',
'value4'], ['subobject1', [['key1', 'value1'], ['key2', 'value2'], ['key3', 'value3']]], ['#key5', 'value5'], ['key6', 'v_a_l_u_e_6'], ['subobject2', [['key1', 'value1']]], ['key7', 'value7'], ['key8', 'null']]]]
[0]:
['object1', [['key1', 'value1'], ['key2', 'null'], ['#key3', 'value3'], ['key4', 'value4'], ['subobject1', [['key1', 'value1'], ['key2', 'value2'], ['key3', 'value3']]], ['#key5', 'value5'], ['key6', 'v_a_l_u_e_6'], ['subobject2', [['key1', 'value1']]], ['key7', 'value7'], ['key8', 'null']]]
- obj_name: 'object1'
- objects: [['key1', 'value1'], ['key2', 'null'], ['#key3', 'value3'], ['key4',
'value4'], ['subobject1', [['key1', 'value1'], ['key2', 'value2'], ['key3', 'value3']]], ['#key5', 'value5'], ['key6', 'v_a_l_u_e_6'], ['subobject2', [['key1', 'value1']]], ['key7', 'value7'], ['key8', 'null']]
[0]:
['key1', 'value1']
- key: 'key1'
- val: 'value1'
[1]:
['key2', 'null']
- key: 'key2'
- val: 'null'
[2]:
['#key3', 'value3']
- key: '#key3'
- val: 'value3'
[3]:
['key4', 'value4']
- key: 'key4'
- val: 'value4'
[4]:
['subobject1', [['key1', 'value1'], ['key2', 'value2'], ['key3', 'value3']]]
- obj_name: 'subobject1'
- objects: [['key1', 'value1'], ['key2', 'value2'], ['key3', 'value3']]
[0]:
['key1', 'value1']
- key: 'key1'
- val: 'value1'
[1]:
['key2', 'value2']
- key: 'key2'
- val: 'value2'
[2]:
['key3', 'value3']
- key: 'key3'
- val: 'value3'
[5]:
['#key5', 'value5']
- key: '#key5'
- val: 'value5'
[6]:
['key6', 'v_a_l_u_e_6']
- key: 'key6'
- val: 'v_a_l_u_e_6'
[7]:
['subobject2', [['key1', 'value1']]]
- obj_name: 'subobject2'
- objects: [['key1', 'value1']]
[0]:
['key1', 'value1']
- key: 'key1'
- val: 'value1'
[8]:
['key7', 'value7']
- key: 'key7'
- val: 'value7'
[9]:
['key8', 'null']
- key: 'key8'
- val: 'null'

但我无法将 setResultsName 改为 [0] 索引:

obj << Group(name_expr('obj_name') + objects('objects'))('section')

返回错误的结果。

最佳答案

这应该能帮到你。有关详细信息,请参阅评论。

from pyparsing import *

test_string ='''
object1 {
key1 = value1
key2
#key3 = value3
key4
#key5 = value5
key6 = value6
subobject1 {
key1 = value1
key2 = value2
key3 = value3
}
}'''

# interpret inline 'string' as Suppress('string'),
# instead of LBRACE,EQ,RBRACE,HASH = map(Suppress, '{=}#')
ParserElement.inlineLiteralsUsing(Suppress)

# be sure to exclude special characters when using printables
name_expr = Word(printables, excludeChars='{}')
key_val_expr = '=' + Word(printables)

# p1('name') is equivalent to p1.setResultsName('name')
# p1 | p2 is equivalent to MatchFirst(p1, p2)
# if lineEnd() matches first, there is no value.
# then use a parse action to return the string 'NONE' as value instead
# else, match a regular key_value
# also, you have to use Group because key_val_line is a repeating element
key_val_line = Group(name_expr('key') + (lineEnd().setParseAction(lambda t: 'NONE') | key_val_expr)('val'))
key_val_lines = OneOrMore(key_val_line)('key_val_lines')

obj = Forward()
obj << Group(name_expr('obj_name') + '{' + OneOrMore(key_val_lines | obj) + '}')('objects')

parse_results = obj.parseString(test_string)
print(parse_results.dump())

这将打印以下内容:

[['object1', ['key1', 'value1'], ['key2', 'NONE'], ['#key3', 'value3'], ['key4', 'NONE'], ['#key5', 'value5'], ['key6', 'value6'], ['subobject1', ['key1', 'value1'], ['key2', 'value2'], ['key3', 'value3']]]]
- objects: ['object1', ['key1', 'value1'], ['key2', 'NONE'], ['#key3', 'value3'], ['key4', 'NONE'], ['#key5', 'value5'], ['key6', 'value6'], ['subobject1', ['key1', 'value1'], ['key2', 'value2'], ['key3', 'value3']]]
- key_val_lines: [['key1', 'value1'], ['key2', 'NONE'], ['#key3', 'value3'], ['key4', 'NONE'], ['#key5', 'value5'], ['key6', 'value6']]
[0]:
['key1', 'value1']
- key: 'key1'
- val: 'value1'
[1]:
['key2', 'NONE']
- key: 'key2'
- val: 'NONE'
[2]:
['#key3', 'value3']
- key: '#key3'
- val: 'value3'
[3]:
['key4', 'NONE']
- key: 'key4'
- val: 'NONE'
[4]:
['#key5', 'value5']
- key: '#key5'
- val: 'value5'
[5]:
['key6', 'value6']
- key: 'key6'
- val: 'value6'
- obj_name: 'object1'
- objects: ['subobject1', ['key1', 'value1'], ['key2', 'value2'], ['key3', 'value3']]
- key_val_lines: [['key1', 'value1'], ['key2', 'value2'], ['key3', 'value3']]
[0]:
['key1', 'value1']
- key: 'key1'
- val: 'value1'
[1]:
['key2', 'value2']
- key: 'key2'
- val: 'value2'
[2]:
['key3', 'value3']
- key: 'key3'
- val: 'value3'
- obj_name: 'subobject1'

关于python - Pyparsing:使用键=值解析嵌套明文数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51047415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com