Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'yet.': 0.04; 'argument': 0.05; 'that?': 0.05; "'',": 0.07; 'removes': 0.07; 'string': 0.09; 'subject:string': 0.09; 'toss': 0.09; 'subject:How': 0.10; 'cc:addr:python-list': 0.11; 'python': 0.11; '>>': 0.16; '18:13,': 0.16; 'element,': 0.16; 'newlines': 0.16; 'runs,': 0.16; 'separated': 0.16; 'smith"': 0.16; 'stringio': 0.16; 'subject:remove': 0.16; 'index': 0.16; 'thanks,': 0.17; 'wrote:': 0.18; 'looked': 0.18; 'everyone,': 0.19; 'properly': 0.19; "skip:' 30": 0.19; 'import': 0.22; 'email addr:gmail.com>': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'finally,': 0.24; 'mon,': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; '>': 0.26; 'first,': 0.26; 'this:': 0.26; 'header:In-Reply- To:1': 0.27; 'rest': 0.29; 'chris': 0.29; 'am,': 0.29; "doesn't": 0.30; 'skip:& 60': 0.30; 'message-id:@mail.gmail.com': 0.30; 'skip:( 20': 0.30; 'along': 0.30; "i'm": 0.30; 'url:mailman': 0.30; 'sep': 0.31; 'anyone': 0.31; 'probably': 0.32; 'figure': 0.32; 'worked': 0.33; 'url:python': 0.33; 'subject:the': 0.34; "can't": 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'url:listinfo': 0.36; 'doing': 0.36; "i'll": 0.36; 'url:org': 0.36; 'should': 0.36; 'example,': 0.37; 'list': 0.37; 'implement': 0.38; 'thank': 0.38; 'easiest': 0.38; 'pm,': 0.38; 'skip:& 20': 0.39; 'does': 0.39; 'subject:can': 0.39; 'url:mail': 0.40; 'how': 0.40; 'remove': 0.60; 'easy': 0.60; "you're": 0.61; 'first': 0.61; 'making': 0.63; 'charset:windows-1252': 0.65; 'study': 0.69; 'limit': 0.70; 'article': 0.77; 'difference.': 0.84; '\xa0but,': 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=m3LtSb8hcdjdogOgbLOwywR+h0bxfb5r/Y7/s592MFc=; b=z/4A4P/WoWd25lSKVI4/pLhXlEL3rE1o0NuI3g0yjS8aK0oP8675QH7jpiUcWhfBp1 sY/U1aAfVl8j2NxMep+657NH1Jgkeuh5m060P8DBloEdZ5S1tEOXIO2WyW/z7Ou11PdI V/wP5ONt+IhephMsKhXUuZ3pqlF+KJipauL5XYyv5B3sbB42spW+T4ufmocv2wTiuwe0 zwDYq+UQ7iSmos4a2CrDkZVSwBFRKCDT0YyunQ6CISyzgb5H2jt9mEy1BgI3pKzPMbVI MakIMzSrtXFQsPI960qgS+Umfi43pbrUjivYd/Plk1PzYAtQzuZZiwa/k+bLY81RN4Y8 bp8g== MIME-Version: 1.0 X-Received: by 10.229.250.5 with SMTP id mm5mr2589829qcb.19.1378901298828; Wed, 11 Sep 2013 05:08:18 -0700 (PDT) In-Reply-To: References: <5224B786.2050606@gmail.com> Date: Wed, 11 Sep 2013 13:08:18 +0100 Subject: Re: How can I remove the first line of a multi-line string? (SOLVED) From: =?ISO-8859-1?Q?F=E1bio_Santos?= To: Roy Smith Content-Type: multipart/alternative; boundary=001a113495c4190d8e04e61a7ab3 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 191 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1378901307 news.xs4all.nl 16010 [2001:888:2000:d::a6]:53711 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:53955 --001a113495c4190d8e04e61a7ab3 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable On 2 Sep 2013 18:13, "Roy Smith" wrote: > > In article , > Anthony Papillion wrote: > > > On 09/02/2013 11:12 AM, Chris =93Kwpolska=94 Warrick wrote: > > > On Mon, Sep 2, 2013 at 6:06 PM, Anthony Papillion > > > wrote: > > >> Hello Everyone, > > >> > > >> I have a multi-line string and I need to remove the very first line from > > >> it. How can I do that? I looked at StringIO but I can't seem to figure > > >> out how to properly use it to remove the first line. Basically, I want > > >> to toss the first line but keep everything else. Can anyone put me on > > >> the right path? I know it is probably easy but I'm still learning Python > > >> and don't have all the string functions down yet. > > >> > > >> Thanks, > > >> Anthony > > >> -- > > >> http://mail.python.org/mailman/listinfo/python-list > > > > > > Use split() and join() methods of strings, along with slicing. Like this: > > > > > > fullstring =3D """foo > > > bar > > > baz""" > > > > > > sansfirstline =3D '\n'.join(fullstring.split('\n')[1:]) > > > > > > The last line does this: > > > 1. fullstring.split('\n') turns it into a list of ['foo', 'bar', 'baz'] > > > 2. the [1:] slice removes the first element, making it ['bar', 'baz'] > > > 3. Finally, '\n'.join() turns the list into a string separated by > > > newlines ("""bar > > > baz""") > > > > This, of course, worked like a charm. I really need to study the string > > methods. In the work I'm doing they are going to come in very handy. > > Thank you, Chris! > > Let me toss out a couple of other possibilities. Not necessarily > better, but if you're learning about strings, you might as well learn > some other ways to do it: > > > s =3D """foo > > bar > > baz""" > > print "using index..." > i =3D s.index('\n') > print s[i+1:] > > print "using regex..." > import re > print re.sub(r'^[^\n]*\n', '', s) > > > I'll admit, the split/slice/join solution is probably the easiest to > implement (and to understand when you're reading the code). But, it > copies all the data twice; once when split() runs, and again when join() > runs. Both the index and regex solutions should only do a single copy. > For huge strings, this might matter. For a three-liner as in your > example, it doesn't make any difference. > Is there not a limit argument to str.split? This should be trivial. first, rest =3D multiline_str.split('\n', 1) --001a113495c4190d8e04e61a7ab3 Content-Type: text/html; charset=windows-1252 Content-Transfer-Encoding: quoted-printable


On 2 Sep 2013 18:13, "Roy Smith" <roy@panix.com> wrote:
>
> In article <mailman.500.1378139057.19984.python-list@python.org>= ,
> =A0Anthony Papillion <papill= ion@gmail.com> wrote:
>
> > On 09/02/2013 11:12 AM, Chris =93Kwpolska=94 Warrick wrote:
> > > On Mon, Sep 2, 2013 at 6:06 PM, Anthony Papillion <papillion@gmail.com>
> > > wrote:
> > >> Hello Everyone,
> > >>
> > >> I have a multi-line string and I need to remove the very= first line from
> > >> it. How can I do that? I looked at StringIO but I can= 9;t seem to figure
> > >> out how to properly use it to remove the first line. Bas= ically, I want
> > >> to toss the first line but keep everything else. =A0Can = anyone put me on
> > >> the right path? I know it is probably easy but I'm s= till learning Python
> > >> and don't have all the string functions down yet. > > >>
> > >> Thanks,
> > >> Anthony
> > >> --
> > >> http://mail.python.org/mailman/listinfo/python-list
> > >
> > > Use split() and join() methods of strings, along with slicin= g. =A0Like this:
> > >
> > > =A0 =A0 fullstring =3D """foo
> > > =A0 =A0 bar
> > > =A0 =A0 baz"""
> > >
> > > =A0 =A0 sansfirstline =3D '\n'.join(fullstring.split= ('\n')[1:])
> > >
> > > The last line does this:
> > > 1. fullstring.split('\n') turns it into a list of [&= #39;foo', 'bar', 'baz']
> > > 2. the [1:] slice removes the first element, making it ['= ;bar', 'baz']
> > > 3. Finally, '\n'.join() turns the list into a string= separated by
> > > newlines ("""bar
> > > baz""")
> >
> > This, of course, worked like a charm. I really need to study the = string
> > methods. In the work I'm doing they are going to come in very= handy.
> > Thank you, Chris!
>
> Let me toss out a couple of other possibilities. =A0Not necessarily > better, but if you're learning about strings, you might as well le= arn
> some other ways to do it:
>
>
> s =3D """foo
>
> bar
>
> baz"""
>
> print "using index..."
> i =3D s.index('\n')
> print s[i+1:]
>
> print "using regex..."
> import re
> print re.sub(r'^[^\n]*\n', '', s)
>
>
> I'll admit, the split/slice/join solution is probably the easiest = to
> implement (and to understand when you're reading the code). =A0But= , it
> copies all the data twice; once when split() runs, and again when join= ()
> runs. =A0Both the index and regex solutions should only do a single co= py.
> For huge strings, this might matter. =A0For a three-liner as in your > example, it doesn't make any difference.
>

Is there not a limit argument to str.split? This should be t= rivial.

first, rest =3D multiline_str.split('\n', 1)

--001a113495c4190d8e04e61a7ab3--