【问题标题】:Pattern substitution in PythonPython中的模式替换
【发布时间】:2017-06-05 09:10:03
【问题描述】:

寻找一些替代方法来清理包含括号之间信息的表格文件。 这将是包含在管道中的第一步,我需要删除括号内的每个值(包括括号)。

我有什么

> Otu00467  Bacteria(100);Gracilibacteria(99);unclassified(99);unclassified(99);unclassified(99);unclassified(99);
> Otu00469  Bacteria(100);Proteobacteria(96);unclassified(96);unclassified(96);unclassified(96);unclassified(96);
> Otu00470  Bacteria(100);Proteobacteria(100);Alphaproteobacteria(100);Rhodospirillales(100);Rhodospirillaceae(100);Azospirillum(54);

我想要的:

 Otu00467   Bacteria;Gracilibacteria;unclassified;unclassified;unclassified;unclassified;
 Otu00469   Bacteria;Proteobacteria;unclassified;unclassified;unclassified;unclassified;
 Otu00470   Bacteria;Proteobacteria;Alphaproteobacteria;Rhodospirillales;Rhodospirillaceae;Azospirillum;

我的第一种方法是用“;”分割第二列, "(" , ")" 并进一步加入一切。还不错但是太丑了。

谢谢。

【问题讨论】:

    标签: python regex python-2.7


    【解决方案1】:
    import re
    new_string = re.sub(r'\(.*?\)', '', your_string)
    

    【讨论】:

    • 在您的特定情况下应该没有区别,因为没有嵌套括号,但当然这也可以。
    【解决方案2】:

    我会为此尝试正则表达式。类似的东西:

    pattern = re.compile('(\w+)\(\d+\);')
    ';'.join(re.findall(pattern, string))
    

    对于每个字符串

    【讨论】:

      【解决方案3】:

      这个正则表达式去掉了带括号的数字组,它也去掉了任何 '>' 字符,因为您似乎也想消除它们。

      import re
      
      data = '''\
      > Otu00467  Bacteria(100);Gracilibacteria(99);unclassified(99);>unclassified(99);unclassified(99);unclassified(99);
      > Otu00469  Bacteria(100);Proteobacteria(96);unclassified(96);unclassified(96);unclassified(96);unclassified(96);
      > Otu00470  Bacteria(100);Proteobacteria(100);Alphaproteobacteria(100);Rhodospirillales(100);Rhodospirillaceae(100);Azospirillum(54);
      '''
      
      data = re.sub(r'>|\(\d+\)', '', data)
      print(data)
      

      输出

       Otu00467  Bacteria;Gracilibacteria;unclassified;unclassified;unclassified;unclassified;
       Otu00469  Bacteria;Proteobacteria;unclassified;unclassified;unclassified;unclassified;
       Otu00470  Bacteria;Proteobacteria;Alphaproteobacteria;Rhodospirillales;Rhodospirillaceae;Azospirillum;
      

      此代码适用于 Python 2 和 3。

      【讨论】:

        【解决方案4】:

        使用re.sub:

        import re
        
        with open open('file.txt') as file:
            text = re.sub(r'\(.*?\)', '', file.read(), flags=re.M)
        

        这将删除所有出现在括号中的文本。 re.M 标志是多行说明符,当您的字符串在匹配模式中包含换行符时,这很有用。

        【讨论】:

        • 谢谢。已编辑。
        • m 标志只会改变 ^$ 锚点的行为,因此它对这个正则表达式完全没有影响。
        • OTOH,在指定正则表达式时始终谨慎使用r'' 语法以避免反斜杠出现意外问题。
        • @Rawing 好点。我和re.S 混淆了,又名re.DOTALL :oops:
        【解决方案5】:
        #Use re module to use regex
        import re
        
        #Open file and read data in data variable
        data = open('file.txt').read()
        
        #Apply search and replace on data variable
        data = re.sub('\(\d+\)', '', data)
        
        #Print data to output.txt file
        with open('output.txt', 'w') as out:
            out.write(data)
        

        【讨论】:

          猜你喜欢
          • 2016-10-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-03-05
          • 1970-01-01
          • 2014-05-28
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多