69758

String search and write into a file in jython

Question:

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 <strong>line and the above line of it</strong> 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()

Answer1:

one way

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

Recommend

  • IntelliJ 14 Ultimate - Can not add framework
  • UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position
  • How to stop tomcat 7 with maven in eclipse
  • In powershell, using the export-csv cmdlet, my ints are being encapsulated by quotes any idea why?
  • Enterprise Architect Synchronize with Code
  • Do I have to rewrite an html header everytime I want to use it?
  • Grouping by blank nodes
  • Retrieving values from a PHP Multi-dimensional Array
  • Selectively hide background elements when overlayed with transparent div
  • JSR-330 support in Picocontainer : @Inject … @Named(\"xxx)
  • Redshift Querying: error xx000 disk full redshift
  • SAVE attribute needed for Fortran variables when only the C_LOC address is returned to a C program?
  • Insert new calendar with SyncAdapter- Calendar API Android
  • Ensure fsync did its job
  • Textfile Structure (tables)
  • Rails Find when some params will be blank
  • preg_replace Double Spaces to tab (\\t) at the beginning of a line
  • Unity3D & Android: Difference between “UnityMain” and “main” threads?
  • Google Custom Search with transparent background
  • Extracting HTML between tags
  • MongoDB in PHP using aggregate to group by _id is null not working
  • Insert into database using onclick function
  • Deselecting radio buttons while keeping the View Model in synch
  • What is Eclipse's Declaration View used for?
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Excel - Autoshape get it's name from cell (value)
  • Check if a string to interpolate provides expected placeholders
  • Can I make an Android app that runs a web view in Chrome 39?
  • How can I estimate amount of memory left with calling System.gc()?
  • Why winpcap requires both .lib and .dll to run?
  • Return words with double consecutive letters
  • RestKit - RKRequestDelegate does not exist
  • Traverse Array and Display in markup
  • Python: how to group similar lists together in a list of lists?
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • SQL merge duplicate rows and join values that are different
  • LevelDB C iterator
  • Can't mass-assign protected attributes when import data from csv file
  • Busy indicator not showing up in wpf window [duplicate]
  • How can i traverse a binary tree from right to left in java?