因此,我试图编写一个脚本,一旦找到某个单词,就会读取一个文件并提取2个值。在这种情况下,当遇到字符串'exon‘时,它将保存它后面的两个整数。
我从创建空字符串开始:
exon_start = []
exon_end = []下面是我使用的简化数据的一个示例:
for line in data:
print data
>>>
exon 1..35
/gene="CDKN1A"
CDS 73..567
/translation="MSEPAGDVRQNPCGSKACRRLFGPVDSEQLSRDCDALMAGCIQE
ARERWNFDFVTETPLEGDFAWERVRGLGLPKLYLPTGPRRGRDELGGGRRPGTSPALL
QGTAEEDHVDLSLSCTLVPRSGEQAEGSPGGPGDSQGRKRRQTSMTDFYHSKRRLIFS
KRKP"
misc_feature 76..78
/gene="CDKN1A"
exon 518..2106
/gene="CDKN1A"我尝试为re.findall()函数导入正则表达式模块:
indx_exon = range(0,len(data))
# so this relates each line of the data to a specific number in the index我在识别每个单独行中的“exon”短语时遇到了困难,首先,我试图识别文本的哪一行具有外显子序列,以查看re.findall()是否有效,并指出:
for p,line in zip(indx_line,data):
if re.findall(r'exon',line) is True:
print p我什么都没有
当我说:
for p,line in zip(indx_line,data):
exon_test = re.findall(r'exon',line)
print exon_test我得到了一堆不包含“外显子”的线条,而对于“外显子”,它们给了我“外显子”。因此,我知道我可以使用re.findall()特性来查找每个字符串中的每一个外显子
--我只需要弄清楚,当它找到'exon‘时,我会怎么说,它需要在这一行中查找,直到它找到’.‘然后将其旁边的整数附加到相应的列表;即
exon_start = [1,518]
exon_end = [35,2106]发布于 2012-09-23 22:12:31
问题在if re.findall(r'exon',line) is True:行中。因为re.finall()不会返回True或False。示例:
>>> mystr = '123 exon'
>>> import re
>>> re.findall(r'exon', mystr)
['exon']
>>> re.findall(r'exon', mystr) is True
False
>>> bool(re.findall(r'exon',mystr))
True
>>> if re.findall(r'exon', mystr):
... print 'true'
...
true将原始代码更改为:
for p,line in zip(indx_line,data):
if re.findall(r'exon',line):
print p应该让它发挥作用。
编辑:正如@TimPietzcker所指出的,在这种情况下根本不需要使用re。为了解决您的第二个问题,即获得..旁边的号码,下面的代码可能会有所帮助:
>>> line = ' exon 1..35'
>>> if 'exon' in line:
... ranges = line.split()[1].split('..')
... print ranges
...
['1', '35']https://stackoverflow.com/questions/12556584
复制相似问题