Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #53505
| From | Roy Smith <roy@panix.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: How can I remove the first line of a multi-line string? (SOLVED) |
| Date | 2013-09-02 13:07 -0400 |
| Organization | PANIX Public Access Internet and UNIX, NYC |
| Message-ID | <roy-581C0C.13071102092013@news.panix.com> (permalink) |
| References | <5224B786.2050606@gmail.com> <CAMw+j7Lp8v49qqSsq9TzevxNz1jWs=rNNbhYdF8NmPfYSi82GA@mail.gmail.com> <mailman.500.1378139057.19984.python-list@python.org> |
In article <mailman.500.1378139057.19984.python-list@python.org>,
Anthony Papillion <papillion@gmail.com> wrote:
> On 09/02/2013 11:12 AM, Chris “Kwpolska” 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'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 = """foo
> > bar
> > baz"""
> >
> > sansfirstline = '\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 = """foo
bar
baz"""
print "using index..."
i = 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.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Re: How can I remove the first line of a multi-line string? (SOLVED) Anthony Papillion <papillion@gmail.com> - 2013-09-02 11:24 -0500
Re: How can I remove the first line of a multi-line string? (SOLVED) Roy Smith <roy@panix.com> - 2013-09-02 13:07 -0400
Re: How can I remove the first line of a multi-line string? (SOLVED) Fábio Santos <fabiosantosart@gmail.com> - 2013-09-11 13:08 +0100
Re: How can I remove the first line of a multi-line string? (SOLVED) Roy Smith <roy@panix.com> - 2013-09-11 09:03 -0400
Re: How can I remove the first line of a multi-line string? (SOLVED) Terry Reedy <tjreedy@udel.edu> - 2013-09-11 08:42 -0400
csiph-web