Hi,

I am embedding python code into an existing legacy classic asp
application. I need to access utf-8 encoded unicode query string data
from python.

I have something along the lines of (simplified):

<script language="python" runat="server">
# have set in IIS7 ASP configuration for default session code page to
be CodePage = 65001
a = unicode(Request('ErrMsg'))
</script>

in test.asp and I use a get request to:

http://localhost/test.asp?ErrMsg=La+connexion+%C3%A0+la+base+de+donn%C3%A9es+a+%C3%A9chou%C3%A9

If I run the the raw query parameter through urllib.unquote_plus, I
get a correctly utf-8 encoded string. If I attempt to get a python
unicode object I get:
Python ActiveX Scripting Engine error '80020009'

Traceback (most recent call last): File "<Script Block >", line 3, in
<module> a = unicode(Request('ErrMsg')) File
"C:\Python26\lib\site-packages\win32com\client\dynamic.py", line 201,
in __str__ return str(self.__call__()) UnicodeEncodeError: 'ascii'
codec can't encode character u'\xe0' in position 13: ordinal not in
range(128)

/test_python.asp, line 5

a = unicode(Request('ErrMsg'))

I think this is because there is no direct method that win32com
provides to get to a unicode object so the unicode built in first
attempts to convert it to a string which results in the com Unicode
string being encoded in ascii and failing on à (\xe0 or %C3%A0 in
utf-8).



The 'Quick Start to Server side COM and Python' document in the
PyWin32 Help file says:
"""
At this stage, Python and Unicode don’t really work well together. All
strings which come from COM will actually be Unicode objects rather
than string objects.

To make this code work in a COM environment, the last line of the
"Hello" method must become:

return "Hello" + " " * self.softspace + str(who)

Note the conversion of the "who" to "str(who)". This forces the
Unicode object into a native Python string object.
"""

Which is totally unhelpful in the case where you have a real Unicode
string and want to get the unicode characters out of it. Is there a
way to do this? How do I get a unicode string?

Thanks in advance for any help or guidance,
Chris


-- 
Christopher Lambacher
ch...@kateandchris.net
_______________________________________________
python-win32 mailing list
python-win32@python.org
http://mail.python.org/mailman/listinfo/python-win32

Reply via email to