python - 在Python中搜索特定模式并删除一行中的模式
问题描述
我在每一行的开头都有特定的模式。我想删除那个特定的模式而不是 python 中的完整行。从实际文件中检索后,我的数据看起来像
>homo_seg-Val-abc-1-1
>homo_seg-Beg-cdf-2-1
>homo_seg-Try-gfh-3-2
>homo_seg-Fuss-cdh-3-1
在这里,我想从数据集中删除“>homo_seg-”并仅保留以下内容
Val-abc-1-1
Beg-cdf-2-1
Try-gfh-3-2
Fuss-cdh-3-1
我可以在 perl 中做到这一点
$new =~s/homo_seg-//g;
我的代码是:
import sys
inFile = sys.argv[1]
with open(inFile) as fasta:
for line in fasta:
if line.startswith('>'):
header = line.split()
t = header[0]
import re # from below answer
regex = r">homo_seg-"
subst = ""
result = re.sub(regex, subst, t, 0, re.MULTILINE)
print(result)
此代码仅给出最后一行的输出。我知道它的一些小错误,但无法拿起它。
解决方案
尝试这个:
new_line = old_line[9:]
或者如果您想更加安全:
if old_line.startswith('homo_seg-') :
new_line = old_line[9:]
推荐阅读
- c++ - 如何在 C++ 中连接多个 C 风格的字符串?
- flutter - Stack 内的 ListView.builder 返回 Null Check 错误用于空值
- c++ - 什么是 C++ 中的数据覆盖?
- stripe-payments - 如何确保支付卡完全被授权支付课外费用?
- reactjs - 当我单击一个按钮时,我需要转到另一个页面,我正在使用 useHistory 挂钩,但它在 React 中无法正常工作
- javascript - 如何在firebase实时数据库中实现promise?
- reactjs - 来自 JSON 文件的动态视图
- python - fxcmpy 连接断开并且无法重新连接
- javascript - 根据 reactjs 中的值获取唯一键
- c# - Telerik 报告 ASP.NET MVC