[英]How do I get my dictionary to print out more values from my regex loop instead of printing only the last one?
Im still a very beginner coder and i have been using python to learn about regexes and output them into.txt files我仍然是一个非常初学者的编码器,我一直在使用 python 来学习正则表达式和 output 将它们转换成.txt 文件
This is what i have so far这是我到目前为止
{python bizarre_protein,echo=T,eval=T}
bizarre_protein = "MTLWARPSSKRGWYWHIRSSSHEEEGYFVWEEPSTLAVSFLYCWHIPSWHATSWHIRSSSRVADEGWRAPSPLYW"
import re
pattern = re.compile("[W][A-Z][A-Z][P|R|N][S]{1}")
for m in re.finditer(pattern, bizarre_protein):
print(m.start(),m.end(),m.group(0))
#start with pattern find W then add 2 A-Z, P|R|N and the S
some_protein = {"motif_start": [m.start(), m.start(), m.start(), m.start(), m.start()], "motif_sequence":[m.group(0), m.group(0), m.group(0), m.group(0), m.group(0)]}
text_lines = [ ]
text_line = "index\t"
for column in some_protein.keys():
text_line = text_line + column + "\t"
print(text_line)
text_lines.append(text_line)
for i in range(0,len(some_protein[column])):
text_line= str(i) + "\t"
for column in some_protein.keys():
text_line += str(some_protein[column][i])
text_line += "\t"
print(text_line)
text_lines.append(text_line)
out_handle = open("bizarre_protein.txt","w")
for line in text_lines:
line = line.rstrip("\t")
print(line)
line = line + "\n"
ignoreme = out_handle.write(line)
ignoreme = out_handle.close()
This is the result I get and it does output into the txt file I created but I need it to output all the rows (3, WARPS - 66, WRAPS) and not just the last one, I tried quite a few things but none of them have worked.这是我得到的结果,它在我创建的 txt 文件中执行 output 但我需要它到 output 所有行(3,WARPS - 66,WRAPS)而不仅仅是最后一个,我尝试了很多东西但没有一个他们工作了。 how do I get it to list all of the rows instead of just the last one, thanks in advance
我如何让它列出所有行而不是最后一行,在此先感谢
3 8 WARPS
14 19 WHIRS
29 34 WEEPS
43 48 WHIPS
53 58 WHIRS
66 71 WRAPS
#this is what i need in the txt file ^
index motif_start motif_sequence
0 66 WRAPS
1 66 WRAPS
2 66 WRAPS
3 66 WRAPS
4 66 WRAPS
#this is all i get^
Is this the result you expect?这是你期待的结果吗?
import re
bizarre_protein = "MTLWARPSSKRGWYWHIRSSSHEEEGYFVWEEPSTLAVSFLYCWHIPSWHATSWHIRSSSRVADEGWRAPSPLYW"
pattern = re.compile("W[A-Z]{1,2}[P|R|N]S")
with open("bizarre_protein.txt", "w") as f:
f.write("index\tmotif_start\tmotif_sequence\n")
for m in re.finditer(pattern, bizarre_protein):
print(m.start(), m.end(), m.group(0))
f.writelines("{}\t{}\t{}\n".format(m.start(), m.end(), m.group(0)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.