[Tutor] Text to HTML question. [string.replace()]

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Sun, 2 Jun 2002 17:02:00 -0700 (PDT)


On Sun, 2 Jun 2002, SA wrote:

>     I have a couple of questions for ya. Remember I'm new to Python and
> am still learning:

No problem!  Let's take a look at your question.


> 1. I have a bunch of text files. I have a python script to generate
> html. I would like to substitute the body of the text files in between
> <body></body> of the html file. I think I can get this part. The trouble
> I'm having is formatting the text body into html. For instance for every
> "\n" in the text body I need to substitute <BR> before placing it into
> the html file body section. And for every "\n\n" I wish to substitute
> <P>. I suppose I nead to use regexp, but how do you call the re module
> and search/sub these patterns?

For text substitution like this, we might be able to get away with just
using the 'replace()' method of strings.  Here's an example of what I
mean:

###
>>> story = """Gully Foyle is my name
... And Terra is my nation
... Deep space is my dwelling place
... And death's my destination."""
>>> new_story = story.replace("And death's", "The stars")
>>> print story
Gully Foyle is my name
And Terra is my nation
Deep space is my dwelling place
And death's my destination.
>>> print new_story
Gully Foyle is my name
And Terra is my nation
Deep space is my dwelling place
The stars my destination.
###



What comes out of replace() is a new string, where all instances of the
thing we want to replace will be substituted.  This leaves the old string
alone though.

If we want to make it look as if the string were being transformed,
in-place, we can do something like this:

###
>>> story = story.replace("And death's", "The stars")
>>> print story
Gully Foyle is my name
And Terra is my nation
Deep space is my dwelling place
The stars my destination.
###

and now 'story' contains the modified story.

You can use string.replace() to solve your text->HTML problem.  Just make
sure to convert all the "\n\n" substrings first --- if you convert all
single '\n' characters first, that wipes out the empty lines!  *grin* So
be careful about the order.


Good luck!