简体   繁体   English

我如何让我的字典从我的正则表达式循环中打印出更多的值,而不是只打印最后一个值?

[英]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.

相关问题 如何打印字典中的选定项目? - How do I print out select items from my dictionary? 如何使用我的 len() 仅在字典中打印我的项目一次而不打印数字 9、9 次 - How to print out my items in my dictionary only once with my len() without it printing the number 9, 9 times 如何获得仅打印一个输出的代码? - How do I get my code to only print one output? 如何打印出一个阶乘数? 我的代码不断打印出太多数字,我不知道为什么? - How do I print out one factorial number? My code keeps printing out too many numbers and I do not know why? 如何打印字典中的最高值 - How do I print the top values in my dictionary 如何从字典值中删除[]和'',以便可以在csv文件中正确打印 - How do I remove [ ] and ' ' from my dictionary values so that I can print it properly in a csv file 如何从另一个python字典中的一个字典中获得相应的列表值,在列表中它们被列为键,比较并打印出csv? - How can I get corresponding list values in one dictionary from another python dictionary where they are listed as keys, compare and print out a csv? 如何从for循环返回一个字符串,而不是逐个打印出来 - how to return a string from a for loop instead of printing out one by one 如何在数组中打印出对值 - How do I print out the pair values in my array 如何获取我的列表以打印出每一行? - How do I get my list to print out every line?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM