Python CGI - UTF-8 doesn't work

前端 未结 2 1104
攒了一身酷
攒了一身酷 2020-12-06 02:54

For HTML5 and Python CGI:

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

Page encoding is UTF-8.

print(\"Con         


        
2条回答
  •  余生分开走
    2020-12-06 03:55

    From https://ru.stackoverflow.com/a/352838/11350

    First dont forget to set encoding in file

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

    Then try

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

    Or if you use apache2, add to your conf.

    AddDefaultCharset UTF-8    
    SetEnv PYTHONIOENCODING utf8
    

提交回复
热议问题