emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* OT: Python help
@ 2010-07-20 10:47 Carsten Dominik
  2010-07-20 11:03 ` Giovanni Ridolfi
  0 siblings, 1 reply; 10+ messages in thread
From: Carsten Dominik @ 2010-07-20 10:47 UTC (permalink / raw)
  To: emacs-org list

Hi there,

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....

Thanks

- Carsten

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: OT: Python help
  2010-07-20 10:47 OT: Python help Carsten Dominik
@ 2010-07-20 11:03 ` Giovanni Ridolfi
  2010-07-20 11:13   ` Carsten Dominik
  0 siblings, 1 reply; 10+ messages in thread
From: Giovanni Ridolfi @ 2010-07-20 11:03 UTC (permalink / raw)
  To: Carsten Dominik; +Cc: emacs-org list

Carsten Dominik <dominik@uva.nl> 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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: OT: Python help
  2010-07-20 11:03 ` Giovanni Ridolfi
@ 2010-07-20 11:13   ` Carsten Dominik
  2010-07-20 11:20     ` Puneeth
  2010-07-20 11:25     ` Giovanni Ridolfi
  0 siblings, 2 replies; 10+ messages in thread
From: Carsten Dominik @ 2010-07-20 11:13 UTC (permalink / raw)
  To: Giovanni Ridolfi; +Cc: emacs-org list


On Jul 20, 2010, at 1:03 PM, Giovanni Ridolfi wrote:

> Carsten Dominik <dominik@uva.nl> 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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: OT: Python help
  2010-07-20 11:13   ` Carsten Dominik
@ 2010-07-20 11:20     ` Puneeth
  2010-07-20 11:25       ` Carsten Dominik
  2010-07-20 11:28       ` Puneeth
  2010-07-20 11:25     ` Giovanni Ridolfi
  1 sibling, 2 replies; 10+ messages in thread
From: Puneeth @ 2010-07-20 11:20 UTC (permalink / raw)
  To: Carsten Dominik; +Cc: emacs-org list

On Tue, Jul 20, 2010 at 4:43 PM, Carsten Dominik <dominik@uva.nl> wrote:
>
> On Jul 20, 2010, at 1:03 PM, Giovanni Ridolfi wrote:
>
>> Carsten Dominik <dominik@uva.nl> 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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: OT: Python help
  2010-07-20 11:13   ` Carsten Dominik
  2010-07-20 11:20     ` Puneeth
@ 2010-07-20 11:25     ` Giovanni Ridolfi
  1 sibling, 0 replies; 10+ messages in thread
From: Giovanni Ridolfi @ 2010-07-20 11:25 UTC (permalink / raw)
  To: Carsten Dominik; +Cc: emacs-org list

Carsten Dominik <dominik@uva.nl> writes:

> On Jul 20, 2010, at 1:03 PM, Giovanni Ridolfi wrote:
>
>> Carsten Dominik <dominik@uva.nl> 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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: OT: Python help
  2010-07-20 11:20     ` Puneeth
@ 2010-07-20 11:25       ` Carsten Dominik
  2010-07-20 11:28       ` Puneeth
  1 sibling, 0 replies; 10+ messages in thread
From: Carsten Dominik @ 2010-07-20 11:25 UTC (permalink / raw)
  To: Puneeth; +Cc: emacs-org list


On Jul 20, 2010, at 1:20 PM, Puneeth wrote:

> On Tue, Jul 20, 2010 at 4:43 PM, Carsten Dominik <dominik@uva.nl>  
> wrote:
>>
>> On Jul 20, 2010, at 1:03 PM, Giovanni Ridolfi wrote:
>>
>>> Carsten Dominik <dominik@uva.nl> 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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: OT: Python help
  2010-07-20 11:20     ` Puneeth
  2010-07-20 11:25       ` Carsten Dominik
@ 2010-07-20 11:28       ` Puneeth
  2010-07-20 13:08         ` Peter Westlake
  1 sibling, 1 reply; 10+ messages in thread
From: Puneeth @ 2010-07-20 11:28 UTC (permalink / raw)
  To: Carsten Dominik; +Cc: emacs-org list

On Tue, Jul 20, 2010 at 4:50 PM, Puneeth <punchagan@gmail.com> wrote:
> On Tue, Jul 20, 2010 at 4:43 PM, Carsten Dominik <dominik@uva.nl> 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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: OT: Python help
  2010-07-20 11:28       ` Puneeth
@ 2010-07-20 13:08         ` Peter Westlake
  2010-07-20 13:12           ` Carsten Dominik
  2010-07-20 18:03           ` Xiao-Yong Jin
  0 siblings, 2 replies; 10+ messages in thread
From: Peter Westlake @ 2010-07-20 13:08 UTC (permalink / raw)
  To: Carsten Dominik; +Cc: emacs-org list



On Tue, 20 Jul 2010 16:58 +0530, "Puneeth" <punchagan@gmail.com> wrote:
> On Tue, Jul 20, 2010 at 4:50 PM, Puneeth <punchagan@gmail.com> wrote:
> > On Tue, Jul 20, 2010 at 4:43 PM, Carsten Dominik <dominik@uva.nl> 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.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: OT: Python help
  2010-07-20 13:08         ` Peter Westlake
@ 2010-07-20 13:12           ` Carsten Dominik
  2010-07-20 18:03           ` Xiao-Yong Jin
  1 sibling, 0 replies; 10+ messages in thread
From: Carsten Dominik @ 2010-07-20 13:12 UTC (permalink / raw)
  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" <punchagan@gmail.com>  
> wrote:
>> On Tue, Jul 20, 2010 at 4:50 PM, Puneeth <punchagan@gmail.com> wrote:
>>> On Tue, Jul 20, 2010 at 4:43 PM, Carsten Dominik <dominik@uva.nl>  
>>> 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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: OT: Python help
  2010-07-20 13:08         ` Peter Westlake
  2010-07-20 13:12           ` Carsten Dominik
@ 2010-07-20 18:03           ` Xiao-Yong Jin
  1 sibling, 0 replies; 10+ messages in thread
From: Xiao-Yong Jin @ 2010-07-20 18:03 UTC (permalink / raw)
  To: Peter Westlake; +Cc: Carsten Dominik, emacs-org list

On Tue, 20 Jul 2010 14:08:32 +0100, Peter Westlake wrote:

> On Tue, 20 Jul 2010 16:58 +0530, "Puneeth" <punchagan@gmail.com> 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.
-- 
J    c/*    __o/*
X    <\     * (__
Y    */\      <

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2010-07-20 18:04 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-07-20 10:47 OT: Python help Carsten Dominik
2010-07-20 11:03 ` Giovanni Ridolfi
2010-07-20 11:13   ` Carsten Dominik
2010-07-20 11:20     ` Puneeth
2010-07-20 11:25       ` Carsten Dominik
2010-07-20 11:28       ` Puneeth
2010-07-20 13:08         ` Peter Westlake
2010-07-20 13:12           ` Carsten Dominik
2010-07-20 18:03           ` Xiao-Yong Jin
2010-07-20 11:25     ` Giovanni Ridolfi

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).