python - Python 中的模式替换

标签 python regex python-2.7

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

我有什么

> 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;

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

谢谢。

最佳答案

import re
new_string = re.sub(r'\(.*?\)', '', your_string)

关于python - Python 中的模式替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44365445/

相关文章:

python - openpyxl:读取 $ 和 .来自 xlsx

python - 以编程方式发送键盘中断

javascript - OpenCV在python和javascript之间调整了大不相同的结果

javascript - 通过JavaScript正则表达式解析自定义数据

javascript - 转义正则表达式字符串?

c# - 使用正则表达式从字符串中抓取标签词

python - 以表格形式打印

python - 为什么Python的多处理Queue有一个buffer和一个Pipe

python - django 2.0.7 NoReverseMatch 在/帖子

python - 字符串和列表的连接