string.replace() can't replace newline characters???

Tim Peters tim_one at email.msn.com
Fri Aug 11 14:30:09 EDT 2000


[posted & mailed]

[Thomas Gagne]
> I have a string with newline characters in it that I want to
> replace with text.  My code goes
>
> value = string.replace(qs[x][0], "\n", "'+char(10)+'")
>
> The result is a new string with the text "'+char(10)+'" **AFTER**
> each newline character.

You're probably suffering from an illusion, but hard to guess what if you
don't show us *why* you think this is happening.  Here's why I think it
isn't:

C:\Python16>python
Python 1.6a2 (#0, Apr  6 2000, 11:45:12) [MSC 32 bit (Intel)] on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import string
>>> test = "1\n2\n3\n"  # test string w/ 3 newlines
>>> test
'1\0122\0123\012'
>>> string.replace(test, "\n", "'+char(10)+'")
"1'+char(10)+'2'+char(10)+'3'+char(10)+'"
>>>

So there are no newlines in the result when I do it.  Show us what happens
when you do it.






More information about the Python-list mailing list