Python Tutorial: double single quotes?

Quinn Dunkan quinn at ngwee.ugcs.caltech.edu
Fri Sep 8 07:08:42 EDT 2000


On Fri, 08 Sep 2000 08:46:34 GMT, Jonadab the Unsightly One
<jonadab at bright.net> wrote:
># Fix stupid ``quotes'' in HTML documents.  I got tired of
># seeing the double single quotation marks, (doubled single
># quotes -- WHY?) in the Python documentation, so I decided
># to FIX it.  Here we go...

Well, in case you're curious as to the "why", it's probably because the python
docs are written in TeX, which uses ``'' to generate smart quotes.  After
writing TeX for a while, I'm pretty used to them and occaisionally put them
into non-TeX docs because my fingers are trained.

[ snip perl ]

In perl... on a python newsgroup, no less :)  Just to show you how much
shorter and clearer python can be, here's my version (with some functionality
removed that belongs in the shell anyway):

import sys, string
while 1:
    s = sys.stdin.readline()
    if not s:
        break
    for q in "''", "``":
        s = string.replace(s, q, '"')
    sys.stdout.write(s)

And in case you're stuck on a system without a shell (macos?), here's the
python equivalent of your program:

import glob, string
for f in glob.glob('*.html'):
    s = open(f).read()
    for q in "''", "``":
        s = string.replace(s, q, '"')
    open(f, 'w').write(s)

I've occaisionally wished for a multi-replace strop function, for efficiency's
sake.  i.e.

import glob
for f in glob.glob('*.html'):
    open(f, 'w').write(open(f).read().mreplace(("''", '"'), ("``", '"')))

maybe someone's written one somewhere?
Of course, depending on which order python evaluates expressions it could just
truncate all your files instead, but let's live on the edge a little :)



More information about the Python-list mailing list