Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #53502 > unrolled thread
| Started by | Anthony Papillion <papillion@gmail.com> |
|---|---|
| First post | 2013-09-02 11:24 -0500 |
| Last post | 2013-09-11 08:42 -0400 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
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
| From | Anthony Papillion <papillion@gmail.com> |
|---|---|
| Date | 2013-09-02 11:24 -0500 |
| Subject | Re: How can I remove the first line of a multi-line string? (SOLVED) |
| Message-ID | <mailman.500.1378139057.19984.python-list@python.org> |
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!
Anthony
[toc] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2013-09-02 13:07 -0400 |
| Message-ID | <roy-581C0C.13071102092013@news.panix.com> |
| In reply to | #53502 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Fábio Santos <fabiosantosart@gmail.com> |
|---|---|
| Date | 2013-09-11 13:08 +0100 |
| Message-ID | <mailman.244.1378901307.5461.python-list@python.org> |
| In reply to | #53505 |
[Multipart message — attachments visible in raw view] — view raw
On 2 Sep 2013 18:13, "Roy Smith" <roy@panix.com> wrote:
>
> 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.
>
Is there not a limit argument to str.split? This should be trivial.
first, rest = multiline_str.split('\n', 1)
[toc] | [prev] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2013-09-11 09:03 -0400 |
| Message-ID | <roy-1B8F9C.09032711092013@news.panix.com> |
| In reply to | #53955 |
In article <mailman.244.1378901307.5461.python-list@python.org>,
Fábio Santos <fabiosantosart@gmail.com> wrote:
One suggested solution:
> > > > sansfirstline = '\n'.join(fullstring.split('\n')[1:])
To which I suggested a couple of alternatives:
> > i = s.index('\n')
> > print s[i+1:]
> > [...]
> > 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 = multiline_str.split('\n', 1)
This certainly prevents the extra splitting and re-compositing of the
additional lines.
One thing to watch out for is that split(s, 1) does *at most* 1 split.
If the original string didn't have any newlines, you would end up with a
single-item list and the unpack would fail. You can get around that
with:
rest = multiline_str.split('\n', 1)[-1]
but that's kind of cryptic.
Looking back at my solutions suggested above, s.index() fails in the
same situation (throws ValueError). The regex solution works fine, once
again proving that 1970's technology is still superior to all this
newfangled stuff.
A coworker recently introduced me to what is apparently a classic essay
(http://www.team.net/mjb/hawg.html). If Unix is the Hole Hawg of
operating systems, then regexes are the Hole Hawg of string processing
tools. Those of us who grew up around them, appreciate their power and
have learned how to use them without chopping off any fingers. And we
look on with a mix of amusement and pity at the current crop of
programmers who play with index(), split(), startswith(), rpartition(),
etc and think they're using real tools :-)
For the record, I have never used a Hole Hawg. I do, however, own a
Milwaukee 1/2" drill. Which also comes with the screw-in handle for
two-handed operation. I've had that get away from me a couple of times,
so I figure it's about my limit. Torque is a wonderful thing until the
thing you're drilling into weighs more than you do.
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2013-09-11 08:42 -0400 |
| Message-ID | <mailman.245.1378903390.5461.python-list@python.org> |
| In reply to | #53505 |
On 9/11/2013 8:08 AM, Fábio Santos wrote:
> Is there not a limit argument to str.split? This should be trivial.
Yes, I posted an example using it the first day.
I also showed how to use iter and next with files and avoid copying.
> first, rest = multiline_str.split('\n', 1)
This does not work right is there is no '\n'
Then someone pointed out
first, dummy, rest = partition('\n')
which does work with no '\n'
--
Terry Jan Reedy
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web