Python 解析重复的键,冒号(通过正则表达式或其他文本解析过程)删除的值

Python 解析重复的键,冒号(通过正则表达式或其他文本解析过程)删除的值,python,regex,parsing,Python,Regex,Parsing,我正在使用Python和regex模块。不过,很高兴为以下内容使用其他解析方法。事实上,我尽量避免使用正则表达式 我对解决这个问题所需的python感到满意。我只是在与正则表达式模式作斗争 一些样本数据(每行单独提供): 在这个阶段,我很高兴有一个只考虑父键、值的解决方案,例如 [ { “这是一个键”:“这是一个与此值相关的id,有时只是存在的id,但在某些情况下是一个由多个空格分隔的基本句子”, “第二个键”:“id2描述,我假设只允许一个空格,没有其他关于内容可能包含的假设”, “关联到”:

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

我对解决这个问题所需的python感到满意。我只是在与正则表达式模式作斗争

一些样本数据(每行单独提供):

在这个阶段,我很高兴有一个只考虑父键、值的解决方案,例如

[
{
“这是一个键”:“这是一个与此值相关的id,有时只是存在的id,但在某些情况下是一个由多个空格分隔的基本句子”,
“第二个键”:“id2描述,我假设只允许一个空格,没有其他关于内容可能包含的假设”,
“关联到”:“关联到id”
},
# ...
]
我相信我能找出其余的答案,当我找到答案时,我会提供它作为替代答案


下面是我使用正则表达式尝试的内容

(?(!+)[A-Za-z]*\s?*):(?*)

我认为将上述内容包装在()*中会使用
key:value
重复的键。但它失败了


这几乎达到了我想要的效果,但我无法重复
键:value
重复,

请尝试下面的正则表达式。这将重复获取
id
说明

(?:(?P.*)\s*?:\s*?(?P\s.*)\s{3,}(?:(?P.*)\s{4,})?*?(?:)?

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

代码

重新导入
a=“””这是一个键:这是一个与此相关的id。此值有时只是存在的id,但在某些情况下是一个由多个空格分隔的基本句子。第二个键:id2 description,我假设只允许一个空格,对内容可能包含的内容没有其他假设
货柜:123ABC
容器:A1B2位于机架6的一些样本数据存储位置:1234考虑另一个随机描述
容器:AbACc123一些更多的样本数据和一些描述存储位置:B14hb14h等等
集装箱:C0Nt41n3r8清理岛9存储地点:DEDE123存储冰箱2
容器:Eb0l4传染病检测架2存放地点:G3nX XOXO专用冰箱4列:8排:3
"""
结果=re.findall((?:(?P.*)\s*?:\s*?(?P\s.*)\s{3,}(?(?P.*)\s{4,})?*?(?:)?,a)
输出=[]
温度={}
对于部分结果:
如果部分[0]:
如果第[1]部分和第[2]部分:
temp1={}
temp1.update({“id”:部分[1]})
temp1.update({“说明”:部分[2]})
elif第[1]部分:
temp1=零件[1]
其他:
通过
如果是temp1:
临时更新({part[0]:temp1})
如果第[3]部分:
临时更新({“关联到”:部分[3]})
output.append(临时)
温度={}
打印(输出)
输出

[
  {
    "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"
  }
]

谢谢你的回答,我想这对我会有用的。对于真实的数据,我只需要确保我断言所有内容都是匹配的,并且所有相关内容都是分组的。我提出了一个更一般的例子,不适合你目前的答案,我正在努力适应这一点<代码>可以包含除2个或更多空格或可选空白字符以外的任何字符的键:val1 val1.1 val1.2 val2(注意前面的2个或更多空格)val2.2 val3 val99+key2:val1 val1.1 val1.2 key99+:val1 val1.1 val1.2 val1.3 val
[
  {
    "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"
  }
]