lstrip problem - beginner question

mstagliamonte madmaxthc at yahoo.it
Tue Jun 4 11:49:45 EDT 2013


On Tuesday, June 4, 2013 11:41:43 AM UTC-4, Fábio Santos wrote:
> On 4 Jun 2013 16:34, "mstagliamonte" <madm... at yahoo.it> wrote:
> 
> >
> 
> > On Tuesday, June 4, 2013 11:21:53 AM UTC-4, mstagliamonte wrote:
> 
> > > Hi everyone,
> 
> > >
> 
> > >
> 
> > >
> 
> > > I am a beginner in python and trying to find my way through... :)
> 
> > >
> 
> > >
> 
> > >
> 
> > > I am writing a script to get numbers from the headers of a text file.
> 
> > >
> 
> > >
> 
> > >
> 
> > > If the header is something like:
> 
> > >
> 
> > > h01 = ('>scaffold_1')
> 
> > >
> 
> > > I just use:
> 
> > >
> 
> > > h01.lstrip('>scaffold_')
> 
> > >
> 
> > > and this returns me '1'
> 
> > >
> 
> > >
> 
> > >
> 
> > > But, if the header is:
> 
> > >
> 
> > > h02: ('>contig-100_0')
> 
> > >
> 
> > > if I use:
> 
> > >
> 
> > > h02.lstrip('>contig-100_')
> 
> > >
> 
> > > this returns me with: ''
> 
> > >
> 
> > > ...basically nothing. What surprises me is that if I do in this other way:
> 
> > >
> 
> > > h02b = h02.lstrip('>contig-100')
> 
> > >
> 
> > > I get h02b = ('_1')
> 
> > >
> 
> > > and subsequently:
> 
> > >
> 
> > > h02b.lstrip('_')
> 
> > >
> 
> > > returns me with: '1' which is what I wanted!
> 
> > >
> 
> > >
> 
> > >
> 
> > > Why is this happening? What am I missing?
> 
> > >
> 
> > >
> 
> > >
> 
> > > Thanks for your help and attention
> 
> > >
> 
> > > Max
> 
> >
> 
> > edit: h02: ('>contig-100_1')
> 
> You don't have to use ('..') to declare a string. Just 'your string' will do.
> 
> You can use str.split to split your string by a character.
> 
> (Not tested)
> 
> string_on_left, numbers = '>contig-100_01'.split('-')
> 
> left_number, right_number = numbers.split('_')
> 
> left_number, right_number = int(left_number), int(right_number)
> 
> Of course, you will want to replace the variable names.
> 
> If you have more advanced parsing needs, you will want to look at regular expressions or blobs.

Thanks, I will try it straight away. Still, I don't understand why the original command is returning me with nothing !? Have you got any idea? 
I am trying to understand a bit the 'nuts and bolts' of what I am doing and this result does not make any sense to me

Regards
Max



More information about the Python-list mailing list