Python CGI - UTF-8 不起作用 [英] Python CGI - UTF-8 doesn't work

查看:24
本文介绍了Python CGI - UTF-8 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于 HTML5 和 Python CGI:

For HTML5 and Python CGI:

如果我编写 UTF-8 元标记,我的代码将无法工作.如果我不写,它会起作用.

If I write UTF-8 Meta Tag, my code doesn't work. If I don't write, it works.

页面编码为 UTF-8.

Page encoding is 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>
    """)

但此代码有效.

推荐答案

来自 https://ru.stackoverflow.com/a/352838/11350

首先不要忘记在文件中设置编码

First dont forget to set encoding in file

#!/usr/bin/env python
# -*- coding: utf-8 -*-

那就试试

import sys
import codecs

sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())

或者,如果您使用 apache2,请添加到您的配置中.

Or if you use apache2, add to your conf.

AddDefaultCharset UTF-8    
SetEnv PYTHONIOENCODING utf8

这篇关于Python CGI - UTF-8 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆