我在 python 上使用正则表达式,下面的代码似乎不起作用
import re
# my regular expressions
exprs = [ r"Gene ID: (.*)\,", r"(.*)\[Homo sapiens]",
r"from:(.*)\s", r"NM_(.*)\.([0-9]+)" ,
r"NP_(.*)\.([0-9]+)\s", r"\,(.*)[^coding]exons",
r"AA length:(.*)\s", r"isoform(.*)\\NP" ]
# search for expressions vector in genetable
with open('massaCHD8.txt', "r") as df:
arq = df.read()
for element in exprs:
resu = re.findall(element, arq, re.M|re.I)
for el in resu:
print(resu.group(0))
当我运行以下脚本时,出现以下错误:
回溯(最近一次调用):文件“io2.py”,第 17 行,在打印(resu.group(0))中 AttributeError: 'list' object has no attribute 'group'
re.findall
返回找到的字符串/元组的列表,而不是 Match 对象。因此,如果要打印匹配项,可以使用:
for element in exprs:
resu = re.findall(element, arq, re.M|re.I)
for el in resu:
print(el if isinstance(el, str) else el[0])
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句