簡體   English   中英

我如何讓我的字典從我的正則表達式循環中打印出更多的值,而不是只打印最后一個值?

[英]How do I get my dictionary to print out more values from my regex loop instead of printing only the last one?

我仍然是一個非常初學者的編碼器,我一直在使用 python 來學習正則表達式和 output 將它們轉換成.txt 文件

這是我到目前為止

{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()

這是我得到的結果,它在我創建的 txt 文件中執行 output 但我需要它到 output 所有行(3,WARPS - 66,WRAPS)而不僅僅是最后一個,我嘗試了很多東西但沒有一個他們工作了。 我如何讓它列出所有行而不是最后一行,在此先感謝

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^

這是你期待的結果嗎?

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM