How to display python Japanese with lolipop

When running Python CGI with Lolipop Apparently, even if I write Japanese in the print statement normally, an error occurs and it cannot be displayed.

Cause

It seems that the standard output is treated as a pipe and the encoding specification is ignored.

wk.py


#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys, locale, codecs

print "Content-Type: text/html\n\n"
print 'sys.stdin.encoding:      %s\n\n' % sys.stdin.encoding
print 'sys.stdout.encoding:     %s\n\n' % sys.stdout.encoding
print '%s\n\n' % sys.getfilesystemencoding()
print '%s\n\n' % sys.getdefaultencoding()
print '%s\n\n' % sys.stdin.encoding
print '%s\n\n' % sys.stdout.encoding
print '%s\n\n' % sys.stderr.encoding

Will produce the following results.

sys.stdin.encoding: None
sys.stdout.encoding: None
ANSI_X3.4-1968
ascii
None
None
None

solution

Forcibly use encoding to change the output destination of the print statement.

#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys, locale, codecs
print "Content-Type: text/html\n\n"

wk = u"Display Japanese"

Writer = codecs.getwriter('utf-8')
stdout = Writer(sys.stdout)
print >>stdout, u"%s" % (wk) 

As a result, the following line is displayed.

Display Japanese

Recommended Posts

How to display python Japanese with lolipop
How to enter Japanese with Python curses
[Python] How to handle Japanese characters with openCV
It's too troublesome to display Japanese with Vim's python3.
Python: How to use async with
How to get started with Python
How to use FTP with Python
How to calculate date with python
How to handle Japanese in Python
[Ev3dev] How to display bmp image on LCD with python
[Python] How to display random numbers (random module)
How to work with BigQuery in Python
How to display multiplication table in python
How to do portmanteau test with python
[Python] How to deal with module errors
How to display Hello world in python
How to install python3 with docker centos
How to not escape Japanese when dealing with json in python
How to display legend marks in one with Python 2D plot
How to install Python
How to install python
How to upload with Heroku, Flask, Python, Git (4)
[python] How to display list elements side by side
How to read a CSV file with Python 2/3
[REAPER] How to play with Reascript in Python
How to do multi-core parallel processing with python
Strategy on how to monetize with Python Java
[Python] How to change the date format (display format)
[Python] How to draw multiple graphs with Matplotlib
[Python] How to read excel file with pandas
How to crop an image with Python + OpenCV
How to specify attributes with Mock of python
How to measure execution time with Python Part 1
How to use tkinter with python in pyenv
How to display images continuously with matplotlib Note
How to make Linux compatible with Japanese keyboard
[Python] How to compare datetime with timezone added
How to measure execution time with Python Part 2
Connect to BigQuery with Python
[2020.8 latest] How to install Python
Send Japanese email with Python3
How to add help to HDA (with Python script bonus)
How to install Python [Windows]
python3: How to use bottle (2)
[Python] How to draw a line graph with Matplotlib
How to do hash calculation with salt in Python
[Introduction to Python] How to iterate with the range function?
Explain in detail how to make sounds with python
[Python] How to use list 1
Connect to Wikipedia with Python
How to update Python Tkinter to 8.6
Post to slack with Python 3
How to upload with Heroku, Flask, Python, Git (Part 3)
How to run tests in bulk with Python unittest
[Python] How to specify the download location with youtube-dl
How to measure mp3 file playback time with python
How to use python interactive mode with git bash
How to use Python argparse
How to update with SQLAlchemy?
How to convert JSON file to CSV file with Python Pandas
How to cast with Theano