String search and write into a file in jython

て烟熏妆下的殇ゞ 提交于 2019-12-11 17:53:08

问题


I wish to write a program that can read a file and if a particular str_to_find is found in a bigger string, say "AACATGCCACCTGAATTGGATGGAATTCATGCGGGACACGCGGATTACACCTATGAGCAGAAATACGGCCTGCGCGATTACCGTGGCGGTGGACGTTCTTCCGCGCGTGAAACCGCGATGCGCGTAGCGGCAGGGGCGATCGCCAAGAAATACCTGGCGGAAAAGTTCGGCATCGAAATCCGCGGCTGCCTGACCCAGATGGGCGACATTCCGCTGGAGATTAAAGACTGGCGTCAGGTTGAGCTTAATCCGTTTTC"

then write that line and the above line of it into the file and keep repeating it for all the match found.

Please suggest the solution. I have written the program for printing that particular search line but I don't know how to write the above line.

import re
import string
file=open('C:/Users/Administrator/Desktop/input.txt','r')
output=open('C:/Users/Administrator/Desktop/output.txt','w')
count_record=file.readline()
str_to_find='AACCATGC'
while count_record:
 if  string.find(list,str_to_find) ==0:
  output.write(count_record)
file.close()
output.close()

回答1:


one way

for line in open("file"):
    if "str_to_find" in line:
        print prev
        print line.rstrip()
    prev=line.rstrip()


来源:https://stackoverflow.com/questions/2504899/string-search-and-write-into-a-file-in-jython

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!