[Tutor] Deleting strings from a line
Martin A. Brown
martin at linux-ip.net
Tue Apr 26 14:14:54 CEST 2011
Greetings,
: I've written a script that scans a biological database and extracts some
: information. A sample of output from my script is as follows:
:
: LYLGILLSHAN AA3R_SHEEP 263 31
:
: LYMGILLSHAN AA3R_HUMAN 264 31
:
: MCLGILLSHAN AA3R_RAT 266 31
:
: LLVGILLSHAN AA3R_RABIT 265 31
:
: The leftmost strings are the ones I want to keep, while I would
: like to get rid of the ones to the right (AA3R_SHEEP, 263 61)
: which are just indicators of where the sequence came from and
: genomic coordinates. Is there any way to do this with a string
: processing command? The loop which builds my list goes like this:
Yes, of course. I would suggest a casual walk through:
http://docs.python.org/library/stdtypes.html#typesseq
This should give you some ideas of the sorts of things you can do
with strings (and other similar such types).
I think what you are looking for is the split() method.
: for line in query_lines:
: if line.startswith('fd;'): # find motif sequences
: #print "Found an FD for your query!", line.rstrip().lstrip('fd;')
: print line.lstrip('fd;')
: motif.append(line.rstrip().lstrip('fd;'))
:
: Is there a del command I can use to preserve only the actual sequences
: themselves. Many thanks in advance!
I see (though it's commented out) that you want to get the result of
line.rstrip().lstrip('fd;') several times. Rather than calculate
this several times, why not store that in a variable. Additionally,
then you can perform other tasks on the intermediate result.
Anyway, try out the following:
line.rstrip().lstrip('fd;').split()[0]
Which, if I were writing in code, I would do, like this:
# -- strip off motif sequences (whatever the heck they are)
#
line = line.rstrip().lstrip('fd;')
# -- break the data into individual, well, units
#
parts = line.split()
# -- print out the sequence
#
print parts[0]
# -- And, here, I still have the various other bits, so I can
# store them in case I want to perform some other sort of
# calculations...
#
my_dict[ parts[0] ] = tuple( parts[1:] )
So, in short, the answer is yes. I think you would benefit from
looking at what sort of string methods there are and what they do.
The strip() method is one of the first to learn. The split() should
be your second. Keep on going, and you'll find all sorts of goodies
in there. And, enjoy Python!
-Martin
--
Martin A. Brown
http://linux-ip.net/
More information about the Tutor
mailing list