[Tutor] FW: wierd replace problem

Walter Prins wprins at gmail.com
Tue Sep 14 23:15:40 CEST 2010


On 14 September 2010 21:10, Roelof Wobben <rwobben at hotmail.com> wrote:

> I understand it but I try to understand why in a file there is this 'word
> python makes a "'word.
>

Python doesn't change what it reads from the file.  However, depending on
how you ask Python to tell you what it's read (or what the contents of a
string variable is), it might display it quoted, and/or include escape
charaters, or not as the case may be.  If what you've read from the file
contains quotes, then obviously you need to be careful to not mistake
Python's quoting of the value of a string as being *part* of that string.
Neither must you mistake the escape character (if applicable) from being
actually part of the string.

For example, consider the following exchange in the Python shell (please try
all of this yourself and experiment):

>>> s = 'blah'
>>> s
'blah'
>>> print s
blah
>>>

I assign the value of 'blah' to the string s. So far simple enough.
Obviosuly the quotes used int the assignment of the string does not form
part of the string itself.  Their utility is only to delineate to Python the
start of the string, and the end of the string.

In the next line I ask Python to evaluate the expression s, which it duly
reporst as 'blah'.  Again, it's using normal Python convention to format the
data as a string, because that's what s is, a string object.  But the quotes
are formatting, they're not really part of the string.

In the next line I ask Python to *print *s.  Now, the true content of s is
printed as it is, and hence you can see that the quotes are not part of the
string.

Now consider the following exchange in the Python shell where I open a file
and write some text to it to prove this point:
>>> f = open('test.txt', 'w+')
>>> f.write('blah')
>>> f.close()
>>> import os
>>> os.system('notepad test.txt')

The last line above opens the text file test.txt in Notepad so you can see
the contents.  As you can see, no quotes or anything else.  Now, while open,
suppose we put a single quote in the file, so it reads:
'blah
...and suppose we then save it and exit notepad so you're back in the Python
shell.  Then we do:

>>> f=open('test.txt','r+')
>>> s=f.read()
>>> f.close()
>>> s
"'blah"

Now I've read the contents of the file back into a string variable s, and
asked Python to evaluate (output) this string object.

Notice, Python is now formatting the string with *doube* quotes (previously
it defaulted to single quotes) to avoid having to escape the single quote
that forms part of the string.  If Python had used single quotes instead,
then there would've been an ambiguity with the single quote that's part of
the string and so it would've had to escape that too.  So consequently it
formats the string with double quotes, which is valid Python syntax and
avoids the backslash. (Stating the obvious, strings can be quoted with
double or single quotes.)  As before, the double quotes, as with the single
quotes earlier, are not part of the string.  They are merely formatting
since Python is being asked to display a string and hence it must indicate
the start and end of the string with suitable quote characters.

Now, as before do:

>>> print s
'blah

As before, with print you see the contents of the string as it is (and as
indeed it is also in the file that you saved). Just the single quote you
added at the front of Blah. No double or single quotes or anything else.

Now finally, let's try something a bit more elaborate.  Do again:

>>> os.system('notepad test.txt')

Then put into the file the following 2 lines of text (notice the file now
contains 2 lines, and both single and double quotes...):
+++"+++This line is double quoted in the file and the quotes have + symbols
around them.+++"+++
---'---This line is single quoted in the file and the quotes have - symbols
around them.---'---

Save it, exit Notepad, then do:
>>> f=open('test.txt', 'r+')
>>> s=f.read()
>>> f.close()
>>> s
'+++"+++This line is double quoted in the file and the quotes have + symbols
around them.+++"+++\n---\'---This line is single quoted in the file and the
quotes have - symbols around them.---\'---\n'
>>> print s
+++"+++This line is double quoted in the file and the quotes have + symbols
around them.+++"+++
---'---This line is single quoted in the file and the quotes have - symbols
around them.---'---

Notice we read both lines in the file into one single string.  See how
Python formats that as a string object, and escapes not only the single
quotes but also the line break characters (\n).  See also when Python is
asked to "print" the string, you can see the escape characters really there.
See what's happened?  Do you understand why?

Walter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100914/46f2b432/attachment.html>


More information about the Tutor mailing list