Path: csiph.com!usenet.pasdenom.info!news.franciliens.net!fdn.fr!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed3.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'yet.': 0.04; 'represents': 0.05; 'that?': 0.05; "'',": 0.07; 'assignment': 0.07; 'differently': 0.07; 'explicit': 0.07; 'string': 0.09; "'')": 0.09; "'a'": 0.09; 'lines.': 0.09; 'lines:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:string': 0.09; 'toss': 0.09; 'try:': 0.09; 'valueerror:': 0.09; 'subject:How': 0.10; 'python': 0.11; 'def': 0.12; 'jan': 0.12; "'a',": 0.16; "('',": 0.16; '(string,': 0.16; '*any*': 0.16; 'expecting': 0.16; 'first:': 0.16; 'ignoring': 0.16; 'indexerror:': 0.16; 'it),': 0.16; 'iterable': 0.16; 'presume': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'rest)': 0.16; 'splitting': 0.16; 'stringio': 0.16; 'subject:remove': 0.16; 'true:': 0.16; 'wrote:': 0.18; 'looked': 0.18; 'first.': 0.19; 'properly': 0.19; 'split': 0.19; '>>>': 0.22; '(in': 0.22; 'handles': 0.22; 'header:User-Agent:1': 0.23; 'string,': 0.24; '---': 0.24; 'equivalent': 0.26; 'first,': 0.26; 'pass': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In- Reply-To:1': 0.27; 'function': 0.29; 'rest': 0.29; "i'm": 0.30; 'lines': 0.31; "skip:' 10": 0.31; 'anyone': 0.31; 'probably': 0.32; 'figure': 0.32; 'text': 0.33; '(including': 0.33; 'subject:the': 0.34; "can't": 0.35; 'except': 0.35; 'but': 0.35; 'false': 0.36; 'next': 0.36; 'subject:?': 0.36; 'to:addr:python- list': 0.38; 'pm,': 0.38; 'subject:can': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'remove': 0.60; 'easy': 0.60; 'catch': 0.60; 'received:173': 0.61; 'simply': 0.61; 'first': 0.61; 'design,': 0.64; 'more': 0.64; 'believe': 0.68; 'line,': 0.68; 'received:fios.verizon.net': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: How can I remove the first line of a multi-line string? Date: Mon, 02 Sep 2013 18:19:17 -0400 References: <5224B786.2050606@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130801 Thunderbird/17.0.8 In-Reply-To: <5224B786.2050606@gmail.com> 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: 65 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1378160377 news.xs4all.nl 15969 [2001:888:2000:d::a6]:42431 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:53534 On 9/2/2013 12:06 PM, Anthony Papillion wrote: > 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. Simply splitting off the first line, using the maxsplit parameter, is more efficient that splitting all lines and joining all but the first. I presume you also want to remove a single line without \n to split on. def keeprest(s): try: return s.split('\n', 1)[1] except IndexError: return '' for s in ('', 'a', 'a\n', 'a\nrest\n'): print('for', repr(s), 'kept:', repr(keeprest(s))) >>> for '' kept: '' for 'a' kept: '' for 'a\n' kept: '' for 'a\nrest\n' kept: 'rest\n' MRAB's s.partition('\n')[2] has exactly the same result as keeprest(s) as, by design, it handles the no-separator case and always returns a triple. I believe it is the equivalent (in C for CPython) of def partition(self, string, splitter): try: first, rest = string.split(splitter, maxsplit=1) return (first, splitter, rest) except ValueError: # from impossible assignment of 1 item to 2 names return (string, '', '') --- If you have *any* iterable of lines and want to process the first line differently than the rest (including just ignoring it), you can use first = True: for line in lines: if first: process_first(line) first = False else: process_rest(line) *or* it = iter(sometext) try: first = next(it) except StopIteration: # always catch this with explicit next pass At this point, *it* now represents the rest of the text and you can pass it to any function expecting an iterable of lines. -- Terry Jan Reedy