Re: [Orgmode] OT: Python help
On Tue, 20 Jul 2010 14:08:32 +0100, Peter Westlake wrote: > On Tue, 20 Jul 2010 16:58 +0530, "Puneeth" wrote: > 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 Although python does not recommend TIMTOWTDI, but I would use the following function s = lambda str: ''.join(['< ' + s for s in str.splitlines(True)]) s("Hello\n\nWorld\n") I think it is much nicer and clearer to me -- probably because I use a lot of haskell. And the following is the function s in haskell s = unlines . map ("< " ++) . lines Just my 2c. -- Jc/*__o/* X<\ * (__ Y*/\ < ___ Emacs-orgmode mailing list Please use `Reply All' to send replies to the list. Emacs-orgmode@gnu.org http://lists.gnu.org/mailman/listinfo/emacs-orgmode
Re: [Orgmode] OT: Python help
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. ___ Emacs-orgmode mailing list Please use `Reply All' to send replies to the list. Emacs-orgmode@gnu.org http://lists.gnu.org/mailman/listinfo/emacs-orgmode
Re: [Orgmode] OT: Python help
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 ___ Emacs-orgmode mailing list Please use `Reply All' to send replies to the list. Emacs-orgmode@gnu.org http://lists.gnu.org/mailman/listinfo/emacs-orgmode
Re: [Orgmode] OT: Python help
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 > HTH, > Puneeth > -- Puneeth ___ Emacs-orgmode mailing list Please use `Reply All' to send replies to the list. Emacs-orgmode@gnu.org http://lists.gnu.org/mailman/listinfo/emacs-orgmode
Re: [Orgmode] OT: Python help
On Jul 20, 2010, at 1:20 PM, Puneeth wrote: On Tue, Jul 20, 2010 at 4:43 PM, Carsten Dominik wrote: On Jul 20, 2010, at 1:03 PM, Giovanni Ridolfi wrote: Carsten Dominik writes: a python question: How do I prefix every line in a multiline string with a string. For example, I would like to add "> " before all lines in a string how about replacing "\n" with "\n > " ? 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> ") Thanks, that does work. - Carsten ___ Emacs-orgmode mailing list Please use `Reply All' to send replies to the list. Emacs-orgmode@gnu.org http://lists.gnu.org/mailman/listinfo/emacs-orgmode
Re: [Orgmode] OT: Python help
Carsten Dominik writes: > On Jul 20, 2010, at 1:03 PM, Giovanni Ridolfi wrote: > >> Carsten Dominik writes: >> >> >>> a python question: How do I prefix every line in a multiline string >>> with a string. For example, I would like to add "> " before all >>> lines >>> in a string >> >> how about replacing "\n" with "\n > " ? > > Please show me the full line of code, I am currently editing a python > script without any knowledge of python... > > :( Carsten , I do not know python as well :-/ I found: 1. from python docs: http://docs.python.org/library/string.html string.replace(str, old, new[, maxreplace]) Return a copy of string str with all occurrences of substring old replaced by new. If the optional argument maxreplace is given, the first maxreplace occurrences are replaced. 2. while googling: "Python like this: python -c 'import sys; print sys.stdin.read().replace("\n", " ")' < days.txt" http://linux.dsplabs.com.au/rmnl-remove-new-line-characters-tr-awk-perl-sed-c-cpp-bash-python-xargs-ghc-ghci-haskell-sam-ssam-p65/ 3. also: http://bytes.com/topic/python/answers/721547-replace-characters-string # >>> s = "sex_m-designer_bw-size_42" # >>> s = s.replace('_', '=') # >>> s = s.replace('-', '&') # >>> s # 'sex=m&designer=bw&size=42' 4. http://stackoverflow.com/questions/930303/python-string-cleanup-manipulation-accented-characters name-of-the-string.replace(' ', '.') # replace spaces with periods hth Giovanni ___ Emacs-orgmode mailing list Please use `Reply All' to send replies to the list. Emacs-orgmode@gnu.org http://lists.gnu.org/mailman/listinfo/emacs-orgmode
Re: [Orgmode] OT: Python help
On Tue, Jul 20, 2010 at 4:43 PM, Carsten Dominik wrote: > > On Jul 20, 2010, at 1:03 PM, Giovanni Ridolfi wrote: > >> Carsten Dominik writes: >> >> >>> a python question: How do I prefix every line in a multiline string >>> with a string. For example, I would like to add "> " before all lines >>> in a string >> >> how about replacing "\n" with "\n > " ? > > 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> ") HTH, Puneeth ___ Emacs-orgmode mailing list Please use `Reply All' to send replies to the list. Emacs-orgmode@gnu.org http://lists.gnu.org/mailman/listinfo/emacs-orgmode
Re: [Orgmode] OT: Python help
On Jul 20, 2010, at 1:03 PM, Giovanni Ridolfi wrote: Carsten Dominik writes: a python question: How do I prefix every line in a multiline string with a string. For example, I would like to add "> " before all lines in a string how about replacing "\n" with "\n > " ? Please show me the full line of code, I am currently editing a python script without any knowledge of python... :( - Carsten ___ Emacs-orgmode mailing list Please use `Reply All' to send replies to the list. Emacs-orgmode@gnu.org http://lists.gnu.org/mailman/listinfo/emacs-orgmode
Re: [Orgmode] OT: Python help
Carsten Dominik writes: > a python question: How do I prefix every line in a multiline string > with a string. For example, I would like to add "> " before all lines > in a string how about replacing "\n" with "\n > " ? Giovanni ___ Emacs-orgmode mailing list Please use `Reply All' to send replies to the list. Emacs-orgmode@gnu.org http://lists.gnu.org/mailman/listinfo/emacs-orgmode