【问题标题】:Parsing a repeated key, value delimetered by a colon (by regex or other text parsing process)解析由冒号分隔的重复键、值(通过正则表达式或其他文本解析过程)
【发布时间】:2020-08-30 09:05:17
【问题描述】:

我正在使用 Python 并使用正则表达式模块。不过,很高兴为以下内容使用替代解析方法。事实上,我尽量避免使用正则表达式。

我对解决问题所需的 python 感到满意。我只是在使用正则表达式模式。

一些示例数据(每行单独提供):

This is a key : this_is_an_id_related_to_this_value   Sometimes just the ID present but in some cases a basic sentence delimited by more than one space          Second key : id2   description where I assume only one space is allowed, no other assumptions on what the content might contain                     <relates_to_id>
Container : 123ABC   <view5>
Container : A1B2   Some Sample Data Located at Rack 6          Storage Place : 1234   Think about another random description                     <view3>
Container : AbACc123   Some more sample data with some description          Storage Place : B14hb14h   Blah Blah Blah                     <view5>
Container : C0Nt41n3r8   Cleanup on isle 9          Storage Place : DEDE123   Storage Fridge 2                     <view8>
Container : Eb0l4   Infectious disease test rack 2          Storage Place : G3nX   XOXO Special Fridge 4                  Col : 8   Row : 3   <view8>

这应该翻译成:

[
  {
    "This is a key": {
      "id": "this_is_an_id_related_to_this_value",
      "description": "Sometimes just the ID present but in some cases a basic sentence delimited by more than one space"
    },
    "Second key": {
      "id": "id2",
      "description": "description where I assume only one space is allowed, no other assumptions on what the content might contain"
    },
    "relates_to": "relates_to_id"
  },
  {
    "Container": {
      "id": "123ABC"
    },
    "relates_to": "view5"
  },
  {
    "Container": {
      "id": "A1B2",
      "description": "Some Sample Data Located at Rack 6"
    },
    "Storage Place": {
      "id": "1234",
      "description": "Think about another random description"
    },
    "relates_to": "view3"
  },
  {
    "Container": {
      "id": "AbACc123",
      "description": "Some more sample data with some description"
    },
    "Storage Place": {
      "id": "B14hb14h",
      "description": "Blah Blah Blah"
    },
    "relates_to": "view5"
  },
  {
    "Container": {
      "id": "C0Nt41n3r8",
      "description": "Cleanup on isle 9"
    },
    "Storage Place": {
      "id": "DEDE123",
      "description": "Storage Fridge 2"
    },
    "relates_to": "view8"
  },
  {
    "Container": {
      "id": "Eb0l4",
      "description": "Infectious disease test rack 2"
    },
    "Storage Place": {
      "id": "G3nX",
      "description": "XOXO Special Fridge 4"
    },
    "Col": 8,
    "Row": 3,
    "relates_to": "view8"
  },
]

在这个阶段,我对只处理父键、值的解决方案感到满意,例如

[
  {
    "This is a key": "this_is_an_id_related_to_this_value   Sometimes just the ID present but in some cases a basic sentence delimited by more than one space",
    "Second key": "id2   description where I assume only one space is allowed, no other assumptions on what the content might contain",
    "relates_to": "relates_to_id"
  },
  # ...
]

我相信我可以弄清楚其余的,当我这样做时,我会提供它作为替代答案。


这是我尝试使用正则表达式的方法,see demo

(?&lt;key&gt;((?! +)[A-Za-z]*\s?)*):(?&lt;value&gt;.*)

我认为将上述内容包含在 ()* 中会成为 key : value 重复的关键。但它失败了,see demo

这几乎得到了我想要的,但我没有重复 key : value 重复,see demo

【问题讨论】:

    标签: python regex parsing


    【解决方案1】:

    请尝试以下正则表达式。这将重复获取keyiddescriptionrelates_to

    (?:(?P<key>.*?)\s*?:\s*?(?P<id>\S.*?)\s{3,}(?:(?P<desc>.*?)\s{4,})?)*?(?:<(?P<relates>.*?)>)?
    

    Regex demo

    请使用以下代码将结果转换为json格式

    代码

    import re
    a="""This is a key : this_is_an_id_related_to_this_value   Sometimes just the ID present but in some cases a basic sentence delimited by more than one space          Second key : id2   description where I assume only one space is allowed, no other assumptions on what the content might contain                     <relates_to_id>
    Container : 123ABC   <view5>
    Container : A1B2   Some Sample Data Located at Rack 6          Storage Place : 1234   Think about another random description                     <view3>
    Container : AbACc123   Some more sample data with some description          Storage Place : B14hb14h   Blah Blah Blah                     <view5>
    Container : C0Nt41n3r8   Cleanup on isle 9          Storage Place : DEDE123   Storage Fridge 2                     <view8>
    Container : Eb0l4   Infectious disease test rack 2          Storage Place : G3nX   XOXO Special Fridge 4                  Col : 8   Row : 3   <view8>
    """
    result=re.findall("(?:(?P<key>.*?)\s*?:\s*?(?P<id>\S.*?)\s{3,}(?:(?P<desc>.*?)\s{4,})?)*?(?:<(?P<relates>.*?)>)?",a)
    output=[]
    temp={}
    for part in result:
        if part[0]:
            if part[1] and part[2]:
                temp1={}
                temp1.update({"id":part[1]})
                temp1.update({"description":part[2]})
            elif part[1]:
                temp1=part[1]
            else:
                pass
            if temp1:
                temp.update({part[0]:temp1})
            if part[3]:
                temp.update({"relates_to":part[3]})
                output.append(temp)
                temp={}
    
    print(output)
    

    输出

    [
      {
        "This is a key": {
          "id": "this_is_an_id_related_to_this_value",
          "description": "Sometimes just the ID present but in some cases a basic sentence delimited by more than one space"
        },
        "Second key": {
          "id": "id2",
          "description": "description where I assume only one space is allowed, no other assumptions on what the content might contain"
        },
        "relates_to": "relates_to_id"
      },
      {
        "Container": "123ABC",
        "relates_to": "view5"
      },
      {
        "Container": {
          "id": "A1B2",
          "description": "Some Sample Data Located at Rack 6"
        },
        "Storage Place": {
          "id": "1234",
          "description": "Think about another random description"
        },
        "relates_to": "view3"
      },
      {
        "Container": {
          "id": "AbACc123",
          "description": "Some more sample data with some description"
        },
        "Storage Place": {
          "id": "B14hb14h",
          "description": "Blah Blah Blah"
        },
        "relates_to": "view5"
      },
      {
        "Container": {
          "id": "C0Nt41n3r8",
          "description": "Cleanup on isle 9"
        },
        "Storage Place": {
          "id": "DEDE123",
          "description": "Storage Fridge 2"
        },
        "relates_to": "view8"
      },
      {
        "Container": {
          "id": "Eb0l4",
          "description": "Infectious disease test rack 2"
        },
        "Storage Place": {
          "id": "G3nX",
          "description": "XOXO Special Fridge 4"
        },
        "Col": "8",
        "Row": "3",
        "relates_to": "view8"
      }
    ]
    

    【讨论】:

    • 感谢您的回答,我认为这对我有用。使用真实数据,我只需要确保我断言所有内容都匹配并且所有相关内容都已分组。我想出了一个不适合您当前答案的更通用的示例,我正在努力适应它。 key which can have anything except 2 or more spaces or an alternative whitespace character : val1 val1.1 val1.2 val2(notice 2 or more spaces preceding) val2.2 val3 val99+ key2 : val1 val1.1 val1.2 key99+ : val1 val1.1 val1.2 val1.3 val... &lt;x&gt;
    猜你喜欢
    • 2022-01-21
    • 2016-06-15
    • 2010-09-19
    • 1970-01-01
    • 1970-01-01
    • 2018-03-20
    • 1970-01-01
    • 2020-10-12
    • 1970-01-01
    相关资源
    最近更新 更多