Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Can't print Chinese to HTTP

Reply
Thread Tools

Can't print Chinese to HTTP

 
 
Dave Angel
Guest
Posts: n/a
 
      12-06-2009
Gnarlodious wrote:
> On Dec 5, 3:54 am, Lie Ryan wrote:
>
>
>> Because of the switch to unicode str, a simple print('晉') should've
>> worked flawlessly if your terminal can accept the character, but the
>> problem is your terminal does not.
>>

>
> There is nothing wrong with Terminal, Mac OSX supports Unicode from
> one end to the other.
> The problem is that your code works normally in Terminal but not in a
> browser.
>
> #!/usr/bin/python
> import sys, io
> print("Content-type:text/plain;charset=f-8\n\n")
> sys.stdout =o.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
> print("晉")
>
> The browser shows "Server error", Apache 2 reports error:
>
> [error] [client 127.0.0.1] malformed header from script. Bad header\xe6\x99\x89: test.py
>
> So far every way to print Unicode to a browser looks very un-Pythonic.
> I am just wondering if I have a bug or am missing the right way
> entirely.
>
> -- Gnarlie
>
>

You change the meaning of sys.stdout without flushing the previous
instance. So of course buffering can mess you up. If you want to
change the encoding, do it at the beginning of the script.

#!/usr/bin/python
import sys, io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
print("Content-type:text/plain;charset=f-8\n\n")
print("晉")


(You probably could use sys.stdout.flush() before reassigning, but doing
it at the beginning is better for several reasons.)

DaveA
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HTTP SOAP/HTTP GET/HTTP POST milan_9211 Software 0 01-10-2011 02:10 PM
print a vs print '%s' % a vs print '%f' a David Cournapeau Python 0 12-30-2008 03:19 AM
Problem - I want to print Current Output of Pdf file and should print once.I get print dialog box but it is not working keto Java 0 05-30-2007 11:27 AM
Unlarging the print to print using PDF file to print Bun Mui Computer Support 3 09-13-2004 03:15 AM
Re: How do I translate a Chinese web site and add Chinese character set to IE..? =A0 - =A0 Taking these two questions one at a time... =A Patrick Dunford NZ Computing 3 04-28-2004 07:15 PM



Advertisments