[Tutor] Problem with IDLE in Windows

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Fri, 7 Sep 2001 10:50:07 -0700 (PDT)


On Fri, 7 Sep 2001, Charlie Clark wrote:

> can anyone tell me why this line causes problems when I try to edit the=
=20
> file in IDLE under windows? It seems that the =B4 is a unicode symbol=20
> that causes IDLE to throw a unicode -> ascii error and delete the file.=
=20
> Does this count as a bug?
>=20
> content =3D "some text"
> content =3D content.replace( "'":"=B4")


Under a regular Python (2.1) shell (without IDLE), this works:

###
>>> content =3D "some text with a ' quote"
>>> content =3D content.replace("'", "")
>>> content
'some text with a \xb4 quote
###


Can anyone confirm that it's IDLE that's doing something weird?


> Any suggestions for an alternative? I need to replace simple
> apostrophes with something else as they get inserted into an SQL
> statement which subsequently doesn't work if they are kept.

Are you using the parameterized style of SQL querying?  If so, then you
don't need to worry so much about properly quoting things.  For example,
instead of:

###
fname, lname =3D 'Issac', 'Asimov'
cursor.execute("""insert into authors (fname, lname)
                  values ('%s', '%s')""" % (fname, lname))
###

we can use:

###
fname, lname =3D 'Issac', 'Asimov'
cursor.execute("""insert into authors (fname, lname)
                  values (?, ?)""", fname, lname)
###

The database handler should then take care of the quoting for you, and
should even escape any apostrophies in strings we insert into the
database.  The topic guide touches on this a little when it talks about
"paramstyle":

    http://python.org/topics/database/DatabaseAPI-2.0.html


Hope this helps!