【问题标题】:pyparsing key value pairs with quotes and line continuationpyparsing 带引号和续行的键值对
【发布时间】:2020-11-08 21:44:49
【问题描述】:

使用 pyparsing 模块,我能够从输入文件中解析键/值对。它们可以如下所示:

key1=value1
key2="value2"
key3="value3 and some more text
"
key4="value4 and ""inserted quotes"" with
more text"

使用以下规则:

eq = Literal('=').suppress()
v1 = QuotedString('"')
v2 = QuotedString('"', multline=True, escQuote='""')
value = Group(v1 | v2)("value")
kv = Group(key + eq + value)("key_value")

我现在遇到了一个问题,即在引用的文本 (!!!) 中使用引号来延续行。请注意,引号在 key_value 对中使用(不是作为转义字符),而是作为连接两个相邻行的方式。

例子:

key5="some more text that is so long that the authors who serialized it to a file thought it"
"would be a good idea to to concatenate strings this way"

有没有办法干净地处理这个问题,或者我应该先尝试识别这些并用另一种连接方法替换这个连接方法?

【问题讨论】:

    标签: concatenation line quotes pyparsing continuations


    【解决方案1】:

    首先,您的v2 表达式实际上是您的v1 表达式的超集。也就是说,任何可以匹配v1 的东西也会匹配v2,所以你真的不需要做value = v1 | v2value = v2 就可以了。

    然后,要处理多个“相邻”引用字符串的情况,而不是解析单个引用字符串,而是解析一个或多个,然后使用解析操作将它们连接起来:

    v2 = OneOrMore(QuotedString('"', multiline=True, escQuote='""'))
    
    # add a parse action to convert multiple matched quoted strings to a single
    # concatenated string
    v2.addParseAction(''.join)
    
    value = v2
    
    # I made a slight change in this expression, moving the results names
    # down into this compositional expression
    kv = Group(key("key") + eq + value("value"))("key_value")
    

    使用此测试代码:

    for parsed_kv in kv.searchString(source):
        print(parsed_kv.dump())
        print()
    

    将打印:

    [['key2', 'value2']]
    - key_value: ['key2', 'value2']
      - key: 'key2'
      - value: 'value2'
    [0]:
      ['key2', 'value2']
      - key: 'key2'
      - value: 'value2'
    
    [['key3', 'value3 and some more text\n']]
    - key_value: ['key3', 'value3 and some more text\n']
      - key: 'key3'
      - value: 'value3 and some more text\n'
    [0]:
      ['key3', 'value3 and some more text\n']
      - key: 'key3'
      - value: 'value3 and some more text\n'
    
    [['key4', 'value4 and "inserted quotes" with\nmore text']]
    - key_value: ['key4', 'value4 and "inserted quotes" with\nmore text']
      - key: 'key4'
      - value: 'value4 and "inserted quotes" with\nmore text'
    [0]:
      ['key4', 'value4 and "inserted quotes" with\nmore text']
      - key: 'key4'
      - value: 'value4 and "inserted quotes" with\nmore text'
    
    [['key5', 'some more text that is so long that the authors who serialized it to a file thought it would be a good idea to to concatenate strings this way']]
    - key_value: ['key5', 'some more text that is so long that the authors who serialized it to a file thought it would be a good idea to to concatenate strings this way']
      - key: 'key5'
      - value: 'some more text that is so long that the authors who serialized it to a file thought it would be a good idea to to concatenate strings this way'
    [0]:
      ['key5', 'some more text that is so long that the authors who serialized it to a file thought it would be a good idea to to concatenate strings this way']
      - key: 'key5'
      - value: 'some more text that is so long that the authors who serialized it to a file thought it would be a good idea to to concatenate strings this way'
    

    【讨论】:

    • 谢谢!再次,非常有见地。并感谢您指出 v1v2 是多余的,现在看起来很明显!
    猜你喜欢
    • 2011-05-19
    • 2011-02-17
    • 1970-01-01
    • 2012-10-11
    • 2014-01-27
    • 2016-12-08
    • 2015-02-18
    • 1970-01-01
    • 2013-04-08
    相关资源
    最近更新 更多