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


Groups > comp.lang.python > #46927

Re: lstrip problem - beginner question

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder1.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'beginner': 0.05; 'removes': 0.07; 'string': 0.09; 'prefix': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'way:': 0.09; 'subject:question': 0.10; 'python': 0.11; 'missing?': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:beginner': 0.16; 'surprises': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'everyone,': 0.19; 'header:User-Agent:1': 0.23; 'headers': 0.24; 'subject:problem': 0.24; 'header': 0.24; 'file.': 0.24; 'script': 0.25; 'equivalent': 0.26; 'header:X-Complaints-To:1': 0.27; 'text': 0.33; 'beginning': 0.33; 'something': 0.35; 'to:addr :python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'remove': 0.60; 'numbers': 0.61; 'encounters': 0.84; 'happening?': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: lstrip problem - beginner question
Date Tue, 04 Jun 2013 17:52:37 +0200
Organization None
References <1829efca-935d-4049-ba61-7138015a2806@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084bb71.dip0.t-ipconnect.de
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2660.1370361157.3114.python-list@python.org> (permalink)
Lines 50
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1370361157 news.xs4all.nl 15871 [2001:888:2000:d::a6]:41642
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:46927

Show key headers only | View raw


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?

"abba".lstrip("ab")

does not remove the prefix "ab" from the string "abba". Instead it removes 
chars from the beginning until it encounters one that is not in "ab". So

t = s.lstrip(chars_to_be_removed)

is roughly equivalent to

t = s
while len(t) > 0 and t[0] in chars_to_be_removed:
    t = t[1:]

If you want to remove a prefix use

s = "abba"
prefix = "ab"
if s.startswith(prefix):
    s = s[len(prefix):]


Back to comp.lang.python | Previous | NextPrevious in thread | Next 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