Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用正则表达式将自由文本解析为dict_Python_Regex_Python 3.x - Fatal编程技术网

Python 使用正则表达式将自由文本解析为dict

Python 使用正则表达式将自由文本解析为dict,python,regex,python-3.x,Python,Regex,Python 3.x,我正试图用正则表达式将这种自由文本转换成字典 丙氨酸A(12000单位/千克)、硫胺素D3(1200单位/千克)、烟碱E(70毫克/千克)、锌(70毫克/千克)、锌(45毫克/千克)、铜(硫酸铜(二)、五水合)(10毫克/千克)、碘(2毫克/千克)、碘酸钙(2毫克/千克)、硫酸钠、无水硫酸钠)[sélénite de Na](0.2 mg/kg),cyaobactin12(0.2%) 我们的想法是抓住关键和价值 键:阿拉明A 值:12000 UI/kg 或 键:铜[五水硫酸铜(II)形态]

我正试图用正则表达式将这种自由文本转换成字典

丙氨酸A(12000单位/千克)、硫胺素D3(1200单位/千克)、烟碱E(70毫克/千克)、锌(70毫克/千克)、锌(45毫克/千克)、铜(硫酸铜(二)、五水合)(10毫克/千克)、碘(2毫克/千克)、碘酸钙(2毫克/千克)、硫酸钠、无水硫酸钠)[sélénite de Na](0.2 mg/kg),cyaobactin12(0.2%)

我们的想法是抓住关键和价值

  • :阿拉明A
  • :12000 UI/kg

  • :铜[五水硫酸铜(II)形态]
  • :10 mg/kg
我尝试使用以下选项来实现此文本转换:

第一种方法:直接使用regex
(()\(\d*\.\s*\d*\s*)(UI\/kg | mg\/kg |%)\)
,但我只能正确地隔离值,而不能隔离键

第二种方法:

  • 去掉括号内的“”,使用这种正则表达式(\[.*),(.\],它不能准确地捕获[sous forme de sulfate de cuivre(II)pentahydrateé]或[sous forme de'iodate de ca**,**酸酐]
  • 沿着“使列表看起来像列表[“alamine A(12000 UI/kg)”,“硫胺素D3(1200 UI/kg)”…] 3.对于列表中的每个元素,使用更简单的正则表达式作为第一种方法
    (.*)\(\d*\.\s*\d*\s*)(UI\/kg | mg\/kg |%)\)

  • 我该如何继续?

    您可以尝试以下方法:

    (?:^|,)(.*?)(\((?:\d*\.*\s*\d*\s*)(?:UI\/kg|mg\/kg|%)\))
    
    当您将其分解时,您将看到键和值的每个“部分”都必须以字符串开头或最后一部分的逗号开头,并带有非捕获组
    (?:^ |,)

    然后,它将使用非贪婪量词
    (.*)\(
    )捕获所有内容,直到下一个左括号。这是您的“键”

    最后,它将利用您现有的代码捕获您的价值,稍加修改:

    (\((?:\d*\.*\s*\d*\s*)(?:UI\/kg|mg\/kg|%)\))
    
    如果要修剪捕获中多余的空格,可以将
    \s*
    添加到密钥组的任一侧:

    (?:^|,)\s*(.*?)\s*(\((?:\d*\.*\s*\d*\s*)(?:UI\/kg|mg\/kg|%)\))
    

    好了,我的朋友!我用正则表达式找到每个结果,然后在最后一个
    )上拆分它们。它覆盖了字符串中的每个异常

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import re
    text = "alamine A (12 000 UI/kg), thiamine D3 (1 200 UI/kg), niacine E (70 mg/kg), zinc [sous forme d'oxyde de zinc] (70 mg/kg), zinc [sous forme de chélate de zinc d'acides aminés, hydraté] (45 mg/kg), copper [sous forme de sulfate de cuivre (II), pentahydraté] (10 mg/kg), iode [sous forme d'iodate de calcium, anhydre] (2 mg/kg), sélénium [sous forme de sélénite de sodium] (0.2 mg/kg), cyaobactin12 (0.2%)"
    my_regex = re.compile(r"([^,]*\[[^\]]*\]\s\([^\)]*\)|[^,]*\([^\)]*\))")
    matches = re.findall(my_regex, text)
    clean_result = []
    for str in matches:
        res = str.rsplit('(', 1)
        clean_result.append((res[0].strip(), res[1][:-1]))
    
    for res in clean_result:
        print "key : " + res[0].decode('utf-8')
        print "value : " + res[1].decode('utf-8')
        print
    
    输出

    key : alamine A
    value : 12 000 UI/kg
    
    key : thiamine D3
    value : 1 200 UI/kg
    
    key : niacine E
    value : 70 mg/kg
    
    key : zinc [sous forme d'oxyde de zinc]
    value : 70 mg/kg
    
    key : zinc [sous forme de chélate de zinc d'acides aminés, hydraté]
    value : 45 mg/kg
    
    key : copper [sous forme de sulfate de cuivre (II), pentahydraté]
    value : 10 mg/kg
    
    key : iode [sous forme d'iodate de calcium, anhydre]
    value : 2 mg/kg
    
    key : sélénium [sous forme de sélénite de sodium]
    value : 0.2 mg/kg
    
    key : cyaobactin12
    value : 0.2%
    

    让我们从更简单的部分开始:值。 它是用括号括起来的:
    (?P\([^)]+\)

    作业即将完成。
    我们将在两个组之间添加一个
    \s
    ,作为它们之间的分隔符。
    最后,让我们来处理序列分隔符:
    (?:(?)?
    
    (?P<value> # Capturing "value" group
      \(       # Matches an opening parentheses
      [^)]+    # Matches one or more non ")" characters
      \)       # Matches a closing parentheses
    )
    
    (?P<key>  # Capturing "key" group
      [^[(]+  # One or more non "(" or "[" characters
      (?:     # Non-capturing group
        \[    # An opening bracket
        [^]]+ # One or more non "]" characters
        \]    # A closing bracket
      )?      # Non-capturing group made optional
    )
    
    (?:        # Non-capturing group
      (?<=,\s) # Either preceded by a coma and a space
      |^       # Or alternatively beginning the string
    )