472,989 Members | 2,649 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,989 software developers and data experts.

Iterating over a file in python

Hello everyone, I wrote a post awhile ago about automating a local client to access a BLAT webserver, but today I have a much easier one. I want to take a batch file and delete every odd line. See below:

Sample File:
>MusmusculusmiR-344
UGAUCUAGCCAAAGCCUGACUGU
>MusmusculusmiR-345
UGCUGACCCCUAGUCCAGUGC
>MusmusculusmiR-346
UGUCUGCCCGAGUGCCUGCCUCU
>MusmusculusmiR-350
UUCACAAAGCCCAUACACUUUCA

I need to delete all the lines that start with '>' and end with a '\n'. I have some code below, but it just isolates the part of the string I want to delete. I need to do the reverse... I know there is some easy way that I am totally missing here!

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/env python
  2. # written 7/28/2007
  3. # by Mark O'Connor
  4.  
  5. def Resize( filename ):
  6.     line = 0
  7.     collect = []
  8.     fp = file( filename )
  9.     data = fp.read()
  10.     fp.close()
  11.     #print data
  12.     while line != -1:
  13.         start = data.find('>', line+1)
  14.         end = data.find ('/n', start)
  15.         chunk = data[start:end]
  16.     return chunk
  17.  
  18.  
Thanks,

Mark
Jul 28 '07 #1
6 5919
bartonc
6,596 Expert 4TB
Hello everyone, I wrote a post awhile ago about automating a local client to access a BLAT webserver, but today I have a much easier one. I want to take a batch file and delete every odd line. See below:

Sample File:
>MusmusculusmiR-344
UGAUCUAGCCAAAGCCUGACUGU
>MusmusculusmiR-345
UGCUGACCCCUAGUCCAGUGC
>MusmusculusmiR-346
UGUCUGCCCGAGUGCCUGCCUCU
>MusmusculusmiR-350
UUCACAAAGCCCAUACACUUUCA

I need to delete all the lines that start with '>' and end with a '\n'. I have some code below, but it just isolates the part of the string I want to delete. I need to do the reverse... I know there is some easy way that I am totally missing here!

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/env python
  2. # written 7/28/2007
  3. # by Mark O'Connor
  4.  
  5. def Resize( filename ):
  6.     line = 0
  7.     collect = []
  8.     fp = file( filename )
  9.     data = fp.read()
  10.     fp.close()
  11.     #print data
  12.     while line != -1:
  13.         start = data.find('>', line+1)
  14.         end = data.find ('/n', start)
  15.         chunk = data[start:end]
  16.     return chunk
  17.  
  18.  
Thanks,

Mark
Hey Mark...
I'd use something like this:
Expand|Select|Wrap|Line Numbers
  1. outList = []
  2. f = open(fileName)
  3. for line in f:
  4.     if line.startswith('>'):
  5.         continue
  6.     outList.append(line)
  7. f.close()
  8. f = open(newFileName, 'w') # or old one to replace it
  9. f.writelines(outLIst)
  10. f.close()
Untested, but generally sound.
Jul 29 '07 #2
Thanks! That did the trick

Mark
Jul 29 '07 #3
bartonc
6,596 Expert 4TB
Thanks! That did the trick

Mark
Files, like all iterators have some pretty cool methods hung on them. That example just scratches the surface.

Any time,
Barton
Jul 29 '07 #4
Or even:

Expand|Select|Wrap|Line Numbers
  1. out = []
  2. with open(fileName) as f:
  3.     for line in f:
  4.         if line.startswith('>'):
  5.             continue
  6.         out.append(line)
  7.  
  8. with open(newFileName, 'w') as f:
  9.     f.writelines(out)
  10.  
Aug 9 '07 #5
twohot
8
@ bartonc

Files, like all iterators have some pretty cool methods hung on them. That example just scratches the surface.
Now I'm listening. What are those cool methods?
Dec 27 '10 #6
Personally, I love python for many reasons, especially when it comes to parsing things and shifting things. This next piece of code should take you to the next level in python development.

Expand|Select|Wrap|Line Numbers
  1. input = open("inputfile.txt")
  2. output = open("outputfile.txt", 'w')
  3.  
  4. output.writelines([(line) for line in input if not line.startswith('>')])
  5.  
And thats it!
Dec 31 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Matthew Wilson | last post by:
I'm playing around with genetic algorithms and I want to write a function that mutates an integer by iterating across the bits, and about 1 in 10 times, it should switch a zero to a one, or a one...
16
by: Derek | last post by:
Pardon the newbie question, but how can I iterate over blocks of data from a binary file (i.e., I can't just iterate over lines, because there may be no end-of-line delimiters at all). Essentially...
2
by: Jesse Noller | last post by:
I am a relative newbie to python and I am having issues trying to iterate over the lines of a file. I have a text file - foo.bar inside of this file are lines of text: x-3411342 y-1324123...
4
by: Fernando Rodríguez | last post by:
Hi, While iterating through a list I'd like to know not just the current element, but also its index. Is there a better way than this: i = 0 newList = for element in aList:...
7
by: Dave Hansen | last post by:
OK, first, I don't often have the time to read this group, so apologies if this is a FAQ, though I couldn't find anything at python.org. Second, this isn't my code. I wouldn't do this. But a...
17
by: News | last post by:
Hi everyone, My goal is to pull command switches/options from a file and then assign the values to select variables which would eventually be included in a class object. The data file looks...
18
by: Drew | last post by:
All - I'm currently writing a toy program as I learn python that acts as a simple address book. I've run across a situation in my search function where I want to iterate across a filtered list....
2
by: glman74 | last post by:
Hi folks, I am trying to tee off both stdout and stderr from a process run through Popen. As a test, I am first trying to print the output below: from subprocess import Popen,PIPE .... p1 =...
13
by: kj | last post by:
Is there a special pythonic idiom for iterating over a list (or tuple) two elements at a time? I mean, other than for i in range(0, len(a), 2): frobnicate(a, a) ?
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.