Help needed with python unicode cgi-bin script

weheh weheh at verizon.net
Tue Dec 11 17:51:38 EST 2007


John & Martin,

Thanks for your help and kind words of encouragement. Still, what you have 
suggested doesn't seem to work, unless I'm not understanding your directive 
to encode as 'windows-1252'. Here's my program in full:

#!C:/Program Files/Python23/python.exe
import cgi, cgitb
import sys, codecs
import os,msvcrt

cgitb.enable()

print u"""Content-Type: text/html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml" lang="en,sp,fr" 
xml:lang="en,sp,fr">
<head>
<meta http-equiv="content-type" content="text/html; charset=windows-1252" />
<meta http-equiv="content-language" content="en,fr,sp" />
</head>
<body>
"""
if sys.platform == 'win32':
 msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
 msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)

x = repr(open('c:/test/spanish.txt','rb').read())
print '<p>',x,'# first print</p>'

x = open('c:/test/spanish.txt','rb').read()
print '<p>',x,'# second print</p>'

x = repr((open('c:/test/spanish.txt','rb').read()).decode('windows-1252'))
print '<p>',x,'# third print</p>'

print """
</body>
</html>
"""


The output of the program is this:

'a\xf1o\r\n' # first print

a?o # second print  #### Note that there is no ñ between the a and the o, 
only a box

u'a\xf1o\r\n' # third print



So what do you advise?





More information about the Python-list mailing list