Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #47010

Re: lstrip problem - beginner question

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!news.giganews.com.POSTED!not-for-mail
NNTP-Posting-Date Tue, 04 Jun 2013 23:53:54 -0500
Date Tue, 04 Jun 2013 21:53:53 -0700
From Larry Hudson <orgnut@yahoo.com>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130329 Thunderbird/17.0.5
MIME-Version 1.0
Newsgroups comp.lang.python
Subject Re: lstrip problem - beginner question
References <1829efca-935d-4049-ba61-7138015a2806@googlegroups.com>
In-Reply-To <1829efca-935d-4049-ba61-7138015a2806@googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
Message-ID <orydnZl-2In_WTPMnZ2dnUVZ_jKdnZ2d@giganews.com> (permalink)
Lines 54
X-Usenet-Provider http://www.giganews.com
X-Trace sv3-S5k4QFHxkBvEXw18YwM/Jf+lbbOa5tqVkruhJzxdd9LIBbpo3Ovyl1Fa2C9PbN4uWmgXxkNHx1VLHJQ!KeuPMwiYZhyN2M8RGNmGPWUIx3fhu/4ac3AG7ttuBhtW6NBjjWwsy82RIZHSoy7T99/P1Qw1Vb0=
X-Complaints-To abuse@giganews.com
X-DMCA-Notifications http://www.giganews.com/info/dmca.html
X-Abuse-and-DMCA-Info Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info Otherwise we will be unable to process your complaint properly
X-Postfilter 1.3.40
X-Original-Bytes 2763
Xref csiph.com comp.lang.python:47010

Show key headers only | View raw


On 06/04/2013 08:21 AM, 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
>

The lstrip() function is the wrong one to use here.  The command help(str.lstrip) gives:

lstrip(...)
     S.lstrip([chars]) -> str

     Return a copy of the string S with leading whitespace removed.
     If chars is given and not None, remove characters in chars instead.

IOW, it does NOT strip the given string, but all the characters in the given string.
So in your second example it (correctly) removes everything and gives you an empty string as the 
result.

One possible alternative is to use slicing:

h02 = '>contig-100_0'
h03 = '>contig-100_'
result = h02[len(h03):]

Or some similar variation, possibly adding a startswith() function for some simple error 
checking.  Of course, other approaches are possible as well,

      -=- Larry -=-

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

lstrip problem - beginner question mstagliamonte <madmaxthc@yahoo.it> - 2013-06-04 08:21 -0700
  Re: lstrip problem - beginner question mstagliamonte <madmaxthc@yahoo.it> - 2013-06-04 08:24 -0700
  Re: lstrip problem - beginner question mstagliamonte <madmaxthc@yahoo.it> - 2013-06-04 08:25 -0700
    Re: lstrip problem - beginner question Fábio Santos <fabiosantosart@gmail.com> - 2013-06-04 16:41 +0100
      Re: lstrip problem - beginner question mstagliamonte <madmaxthc@yahoo.it> - 2013-06-04 08:49 -0700
        Re: lstrip problem - beginner question Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-06-04 17:01 +0100
        Re: lstrip problem - beginner question Dave Angel <davea@davea.name> - 2013-06-04 17:48 -0400
  Re: lstrip problem - beginner question mstagliamonte <madmaxthc@yahoo.it> - 2013-06-04 08:28 -0700
  Re: lstrip problem - beginner question mstagliamonte <madmaxthc@yahoo.it> - 2013-06-04 08:29 -0700
  Re: lstrip problem - beginner question MRAB <python@mrabarnett.plus.com> - 2013-06-04 16:48 +0100
    Re: lstrip problem - beginner question mstagliamonte <madmaxthc@yahoo.it> - 2013-06-04 08:53 -0700
  Re: lstrip problem - beginner question Peter Otten <__peter__@web.de> - 2013-06-04 17:52 +0200
  Re: lstrip problem - beginner question John Gordon <gordon@panix.com> - 2013-06-04 15:55 +0000
    Re: lstrip problem - beginner question mstagliamonte <madmaxthc@yahoo.it> - 2013-06-04 09:06 -0700
  Re: lstrip problem - beginner question Larry Hudson <orgnut@yahoo.com> - 2013-06-04 21:53 -0700

csiph-web