Unicode Hell

Stuart Forsyth stuartf at the-i-junction.com
Fri Nov 7 03:59:30 EST 2003


The replace string in this case is actually the contents of a file.  I
have simplified it for the purposes of the example.  The file I'm doing
the replace on is a web archive (.mht) file.  Within that file are a
number of different replace fields e.g. #name# #organisation# etc..
Everything was working fine until the replace function tried to replace
the #name# replace field with a posting variable that had a tilde in it.
The script then moaned about it being non-ascii and crashed.  The exact
error is:

Error Type:
Python ActiveX Scripting Engine (0x80020009)
Traceback (most recent call last): File "<Script Block >", line 80, in ?
FileContents =
FileContents.replace('Repl_learner',str(Request("learner"))) File
"C:\Python23\lib\site-packages\win32com\client\dynamic.py", line 169, in
__str__ return str(self.__call__()) UnicodeEncodeError: 'ascii' codec
can't encode characters in position 5-9: ordinal not in range(128)

The offending line of code:
FileContents =
FileContents.replace('Repl_learner',str(Request("learner")))


If I change the line of code to 
FileContents = FileContents.replace('Repl_learner',Request("learner"))
I get the following error

Error Type:
Python ActiveX Scripting Engine (0x80020009)
Traceback (most recent call last): File "<Script Block >", line 80, in ?
FileContents = FileContents.replace('Repl_learner',Request("learner"))
TypeError: expected a character buffer object 
/certificate/pycreate.asp, line 104


--------------------------
Stuart Forsyth
the-i-junction

+27 (011) 726 4922
+27 (011) 726 4923 fax


-----Original Message-----
From: Robert Brewer [mailto:fumanchu at amor.org] 
Sent: 07 November 2003 10:38 AM
To: Stuart; python-list at python.org
Subject: RE: Unicode Hell

I assume you've tried the simple solution for string substitution? :

cReplaceString = u"I want to replace %s" % cVal1

You mention Unicode, but don't say what in particular is giving you
problems. If the characters are being output but look garbled, make sure
you check your browser's encoding. For example, in IE6 look at the menu
View->Encoding and select the encoding which matches your page. It's
possible your web page does not specify the encoding, in which case
you'll have to check your server to see what the default encoding is.
You can hint that your web page uses a particular encoding with a tag
within the head element like this:

   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org

> -----Original Message-----
> From: Stuart [mailto:stuartf at the-i-junction.com] 
> Sent: Thursday, November 06, 2003 11:45 PM
> To: python-list at python.org
> Subject: Unicode Hell
> 
> 
> I'm trying to do something seemingly easy yet it is proving incredibly
> frustrating.  Perhaps it is due to my relatively new acquaintance with
> Python.  In a web environment (ASP with python scripting) I 
> have one form
> posting information to another form.  The contents of the 
> posting variable
> (lets call it cVal1 for simplicity) is non ascii characters, 
> lets say éêëìû
> for example.
> 
> 
> 
> On the second page a simple:
> 
> 
> 
> cVal1 = Request("cVal1 ")
> 
> Response.Write(cVal1)
> 
> 
> 
> Writes the values to the screen correctly.
> 
> 
> 
> What I really want to do is replace a field place holder with 
> those values.
> 
> 
> 
> So if we have a string
> 
> 
> 
> cReplaceString = "I want to replace #here#"
> 
> 
> 
> I'd like to replace the #here# with the values of a (or éêëìû)
> 
> 
> 
> Please help
> 
> 
> 
> Regards
> 
> 
> 
> Stuart
> 
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 






More information about the Python-list mailing list