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! -
#!/usr/bin/env python
-
# written 7/28/2007
-
# by Mark O'Connor
-
-
def Resize( filename ):
-
line = 0
-
collect = []
-
fp = file( filename )
-
data = fp.read()
-
fp.close()
-
#print data
-
while line != -1:
-
start = data.find('>', line+1)
-
end = data.find ('/n', start)
-
chunk = data[start:end]
-
return chunk
-
-
Thanks,
Mark
6 5919
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! -
#!/usr/bin/env python
-
# written 7/28/2007
-
# by Mark O'Connor
-
-
def Resize( filename ):
-
line = 0
-
collect = []
-
fp = file( filename )
-
data = fp.read()
-
fp.close()
-
#print data
-
while line != -1:
-
start = data.find('>', line+1)
-
end = data.find ('/n', start)
-
chunk = data[start:end]
-
return chunk
-
-
Thanks,
Mark
Hey Mark...
I'd use something like this: - outList = []
-
f = open(fileName)
-
for line in f:
-
if line.startswith('>'):
-
continue
-
outList.append(line)
-
f.close()
-
f = open(newFileName, 'w') # or old one to replace it
-
f.writelines(outLIst)
-
f.close()
Untested, but generally sound.
Thanks! That did the trick
Mark
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
Or even: -
out = []
-
with open(fileName) as f:
-
for line in f:
-
if line.startswith('>'):
-
continue
-
out.append(line)
-
-
with open(newFileName, 'w') as f:
-
f.writelines(out)
-
@ 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?
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. -
input = open("inputfile.txt")
-
output = open("outputfile.txt", 'w')
-
-
output.writelines([(line) for line in input if not line.startswith('>')])
-
And thats it!
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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:...
|
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...
|
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...
|
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....
|
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 =...
|
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)
?
|
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=()=>{
|
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...
|
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...
|
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...
|
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 :...
|
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...
|
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...
|
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...
|
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...
| |