From mboxrd@z Thu Jan 1 00:00:00 1970 From: Carsten Dominik Subject: Re: OT: Python help Date: Tue, 20 Jul 2010 15:12:00 +0200 Message-ID: <7DE910BA-E35D-4D55-B11B-A875457ABAA5@uva.nl> References: <97ED60B0-D01C-47DB-9B5E-10F1B39E3892@uva.nl><83vd8a1kkv.fsf@yahoo.it> <606EFD47-1329-43F8-8496-1A0B0A2390E9@uva.nl> <1279631312.15948.1385772637@webmail.messagingengine.com> Mime-Version: 1.0 (Apple Message framework v936) Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes Content-Transfer-Encoding: 7bit Return-path: Received: from [140.186.70.92] (port=34791 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1ObCcB-00030F-Ee for emacs-orgmode@gnu.org; Tue, 20 Jul 2010 09:12:08 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1ObCc9-00010r-Vi for emacs-orgmode@gnu.org; Tue, 20 Jul 2010 09:12:07 -0400 Received: from postduif.ic.uva.nl ([145.18.40.180]:42412) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1ObCc9-00010F-NW for emacs-orgmode@gnu.org; Tue, 20 Jul 2010 09:12:05 -0400 In-Reply-To: <1279631312.15948.1385772637@webmail.messagingengine.com> List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Peter Westlake Cc: emacs-org list On Jul 20, 2010, at 3:08 PM, Peter Westlake wrote: > > > On Tue, 20 Jul 2010 16:58 +0530, "Puneeth" > wrote: >> On Tue, Jul 20, 2010 at 4:50 PM, Puneeth wrote: >>> On Tue, Jul 20, 2010 at 4:43 PM, Carsten Dominik >>> wrote: >>>> Please show me the full line of code, I am currently editing a >>>> python script >>>> without any knowledge of python... >>> >>> my_string = "Hello\nWorld" >>> my_new_string = my_string.replace("\n", "\n> ") >> >> Sorry, this code (obviously) doesn't prepend ">" to the first line >> Add this line to do that. >> >> my_new_string = "> " + my_new_string > > Here's a Pythonic way to do it, tested: > > import re > > my_string = "Hello\nWorld" > pattern = re.compile('^',re.MULTILINE) > my_new_string = re.sub(pattern, '> ', my_string) > > This still might not be quite right, as it will turn "Hello\nWorld\n" > into "> Hello\n> World\n> ". Avoid that by using a negative lookahead > for the end of the string: > > my_string = "Hello\n\nWorld\n" > pattern = re.compile('^(?!\Z)',re.MULTILINE) > my_new_string = re.sub(pattern, '> ', my_string) > print my_new_string > > gives: > >> Hello >> >> World > > > Peter. Great. I learned something today. Thanks! - Carsten - Carsten