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