Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.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.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; '16,': 0.03; 'string.': 0.05; 'lines,': 0.07; 'string': 0.09; 'iterate': 0.09; 'subject:characters': 0.09; 'subject:string': 0.09; 'cc:addr :python-list': 0.11; '":"': 0.16; '"some': 0.16; 'formatted': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'hexadecimal': 0.16; 'splitting': 0.16; 'subject:remove': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'split': 0.19; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'characters': 0.30; 'message-id:@mail.gmail.com': 0.30; 'lines': 0.31; 'text': 0.33; 'subject:the': 0.34; "i'd": 0.34; 'subject:from': 0.34; 'convert': 0.35; 'definition': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'method': 0.36; 'changing': 0.37; 'too': 0.37; 'list': 0.37; 'skip:o 20': 0.38; 'work?': 0.38; 'pm,': 0.38; 'remove': 0.60; 'removing': 0.60; 'first': 0.61; 'back': 0.62; 'line,': 0.68; 'million': 0.74; 'as:': 0.81; '2015': 0.84; 'subject:long': 0.84; 'subject:very': 0.91; 'to:none': 0.92 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:cc :content-type; bh=YZu7uXMxgLB3UwwPc/zIHBBR6yLpnOGrSPrbTAgvQ/k=; b=yY7SUwP1fXpYR5Igou3+4o5kHtLMMklA/fj4PbvsJhDS4HI/q6+lus7sfoMyUvx93d QKwnLLUVUWJsp4VWU7pX470QByuN1oTeDFhdPc8FuUoiC6zmngtj4xjR9XXi+5a6CDBD Fn0tJLWI1GX/UVv/asuLNuZfUXlTSQCdHth/4XmsCRVFJjTdLVDvZycWGtVIGKnGAuPV aZi59L6gfFymRqSTpvcMrwo6H0FIxzEFHQ766qsvI47xFJwsGPv7j8LZ0xhXyG6qTlI8 1PBEyqxddq2E4RcKG9t0MXZ7dvwEJ6l9Ep04yoicnAA3vJlv6H00HF2qjBHCgVyC87/M wUuQ== MIME-Version: 1.0 X-Received: by 10.107.134.153 with SMTP id q25mr5095068ioi.27.1431783957825; Sat, 16 May 2015 06:45:57 -0700 (PDT) In-Reply-To: <6a383ce2-5975-4225-b4f2-f744c9d7a516@googlegroups.com> References: <6a383ce2-5975-4225-b4f2-f744c9d7a516@googlegroups.com> Date: Sat, 16 May 2015 23:45:57 +1000 Subject: Re: Fastest way to remove the first x characters from a very long string From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1431783967 news.xs4all.nl 2826 [2001:888:2000:d::a6]:53997 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:90731 On Sat, May 16, 2015 at 11:28 PM, wrote: > I have a string that contains 10 million characters. > > The string is formatted as: > > "0000001 : some hexadecimal text ... \n > 0000002 : some hexadecimal text ... \n > 0000003 : some hexadecimal text ... \n > ... > 0100000 : some hexadecimal text ... \n > 0100001 : some hexadecimal text ... \n" > > and I need the string to look like: > > "some hexadecimal text ... \n > some hexadecimal text ... \n > some hexadecimal text ... \n > ... > some hexadecimal text ... \n > some hexadecimal text ... \n" > > I can split the string at the ":" then iterate through the list removing the first 8 characters then convert back to a string. This method works, but it takes too long to execute. > > Any tricks to remove the first n characters of each line in a string faster? Given that your definition is "each line", what I'd advise is first splitting the string into lines, then changing each line, and then rejoining them into a single string. lines = original_text.split("\n") new_text = "\n".join(line[8:] for line in lines) Would that work? ChrisA