对于HTML5和Python CGI:
如果我写UTF-8元标签,我的代码就不能工作。如果我不写,它就会起作用。
页面编码为UTF-8。
print("Content-type:text/html")
print()
print("""
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
şöğıçü
</body>
</html>
""")这个代码不起作用。
print("Content-type:text/html")
print()
print("""
<!doctype html>
<html>
<head></head>
<body>
şöğıçü
</body>
</html>
""")但是这个代码是有效的。
发布于 2015-05-28 06:53:00
来自https://ru.stackoverflow.com/a/352838/11350
首先,不要忘记在文件中设置编码
#!/usr/bin/env python
# -*- coding: utf-8 -*-然后试一试
import sys
import codecs
sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())或者,如果您使用apache2,则将其添加到您的conf中。
AddDefaultCharset UTF-8
SetEnv PYTHONIOENCODING utf8https://stackoverflow.com/questions/14860034
复制相似问题