[Tutor] String Replace.....

Sheila King sheila@thinkspot.net
Sun, 28 Jan 2001 15:46:49 -0800

On Mon, 29 Jan 2001 00:28:21 +0100, Remco Gerlich <scarblac@pino.selwerd.nl>
wrote about Re: [Tutor] String Replace.....:

:I had written a more detailed reply first, but accidentally sent it to
:Sheila King. Her "SpamCop" system blocked my mail, giving me an URL to open
:it up that didn't work... I'm a bit irritated now and will only give short

I'm surprised that the SpamCop link didn't work. I've tested it myself, and
have had no problems with it. Hmm. (It's not "my" spamcop. It's a service
anyone can subscribe to... http://www.spamcop.net/ )

In any case, I have your original, and much more detail message. Per your
request, via e-mail, I am posting it to the list.

Forwarded message follows.

Sheila King


To: Sheila King <sheila@thinkspot.net>
Subject: Re: [Tutor] String Replace.....
From: Remco Gerlich <scarblac@pino.selwerd.nl>
Date: Mon, 29 Jan 2001 00:22:23 +0100

On Sun, Jan 28, 2001 at 01:31:01PM -0800, Sheila King wrote:
> I'm only a Python newbie, myself, but I've done a fair number of scripts with
> strings in them, and I've never used '\012' in my code, only '\n'.
> Maybe, try replacing the "\012" in your code with "\n"  ???

Start a Python interpreter, and type it in:
>>> "\n"

On Unix, \n is equal to a single LF, which is ASCII 012 (I think that is
octal, so it's 10 decimal, but I'm not sure). String representation
translates control codes like that into octal escapes, iirc.

So they're the same.

> :## Turn Text into HTML
> :
> :    def htmlize(self):
> :        viewdiary = open("c:\diary.txt", 'r')
> :        print viewdiary
> :        print "---------------"
> :        L = viewdiary.readlines()
> :        print L
> :        modified_str = string.replace(L, "\012", "<br>" )
> :        print modified_str
> :        viewdiary.close
> :
> :and the error I'm getting
> :
> :<open file 'c:\diary.txt', mode 'r' at 00B42920>
> :---------------
> :['this is a test\012', 'this is another test\012', 'one more test\012']
> :Exception in Tkinter callback
> :Traceback (most recent call last):
> :  File "c:\python20\lib\lib-tk\Tkinter.py", line 1287, in __call__
> :    return apply(self.func, args)
> :  File "C:\python-code\diary-update\diary.py", line 71, in htmlize
> :    modified_str = string.replace(L, "\012", "<br>" )
> :  File "c:\python20\lib\string.py", line 363, in replace
> :    return s.replace(old, new, maxsplit)
> :AttributeError: replace

You get L by means of readlines(). That function returns a *list* of
strings. You call string.replace(L, ...), which calls L.replace(), but that
function is only defined for strings, not for lists.

Either don't use readlines() but instead use read(), which simple reads in
the whole file as one string, *or* use the list of lines that readlines()
gives you and put them together by means of L.join("<br>") so that the
string.replace call isn't needed anymore.

So either

L = viewdiary.read()
modified_str = string.replace(L, "\n", "<BR>")
# or even modified_str = L.replace("\n", "<BR>")


L = viewdiary.readlines()
modified_str = string.join(L, "<BR>")
# or modified_str = L.join("<BR>")

Or maybe the simplest way is to enclose the whole thing in <pre></pre> tags,
gives a different font, but the browser interprets the line breaks by

Remco Gerlich