[Tutor] string replacement puzzlement

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Thu, 25 Jan 2001 22:33:15 -0800 (PST)


On Thu, 25 Jan 2001, R. A. wrote:

> I'm trying to get the hang of replacing strings within a specified file,
> which I've managed to do with some success.  However, my next move was
> to start trying to replace multiple items within the same file in one
> fell swoop.  The following example is one of my attempts to do so, and
> the results were less than impressive.  In my sample file, containing a
> random collection of punctuation marks, some replacement happened, but
> in each case, the original symbols were left behind, and the "?"
> replacement just didn't happen.  (And I know the code is a kludge, but
> it looked a bit more elegant about 42 changes ago.)

Let's take a look at the code, and see if we can figure out why the
replacement isn't complete:


> import string
> inputfile = open('c:\windows\desktop\\inputfile.txt', 'r')
> outputfile = open('c:\windows\desktop\\outputfile.txt', 'w')
> outputfile.write(inputfile.read().replace("!", "exclamation point"))
> inputfile.seek(0)
> outputfile.write(inputfile.read().replace(".", "period"))
> inputfile.seek(0)
> outputfile.write(inputfile.read().replace("?", "question mark"))
> inputfile.close()
> outputfile.close()

Hmmm... Ok, let's make a small test file, and see how the program affects
it.  We'll do a thought experiment.  Let's say we have the inputfile.txt:

###
!.?
###

What we want in our outputfile is the following:

###
exclamation markperiodquestion mark
###

Now we need to compare what we want with what we get, and once we
understand what's going on, coaxing our program to do the "right" thing
will be easy.  Let's see what happens as we run through the
program.  Let's take an incremental look at what outputfile contains
through sections of your program.


> outputfile.write(inputfile.read().replace("!", "exclamation point"))
> inputfile.seek(0)

Ok, so the output file should contain:

###
exclamation mark.?
###

so far.  Ok, let's continue.


> outputfile.write(inputfile.read().replace(".", "period"))
> inputfile.seek(0)


Ok, now we add the result of that to the outputfile.  Now it looks like
this:

###
exclamation mark.?!period?
###


Finally:

> outputfile.write(inputfile.read().replace("?", "question mark"))


The outputfile should now contain:

###
exclamation mark.?!period?!.question mark
###


(as long as we haven't overlooked anything).


Take a look at why it's doing weird stuff; it shouldn't be too hard to fix
things.  If you have more questions, please feel free to ask.  Good luck!