473,379 Members | 1,367 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 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 5931
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.