[Tutor] Deleting strings from a line

Steven D'Aprano steve at pearwood.info
Tue Apr 26 14:25:01 CEST 2011


Spyros Charonis wrote:
> Hello,
> 
> 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) 

Split each line in multiple words, keeping only the first:

line = "LYLGILLSHAN                      AA3R_SHEEP    263    31"
# split on any whitespace, a maximum of 1 time
head, tail = line.split(None, 1)

head will be "LYLGILLSHAN" and tail will be "AA3R_SHEEP    263    31".

Or, if the text is fixed-width, you can use string slice to extract the 
characters you care about:

head = line[0:11]



-- 
Steven



More information about the Tutor mailing list