我创建了一个 cgi 脚本(使用 apache 在 localhost 上运行),它将从 textarea 加载文本,然后我将使用它。我对 \xc5\xa1、\xc5\xa5、\xc3\xa9 等字符有问题,它们无法正确显示。我尝试了很多方法。这是我的短代码的一个版本,我只是在其中寻找正确的处理方法。
\n#!C:/Python33/python \n# -*- coding: UTF-8 -*-\n \nimport cgi\nimport cgitb\n\ncgitb.enable()\n\nform = cgi.FieldStorage()\nif form.getvalue('textcontent'):\n text_content = form.getvalue('textcontent')\nelse:\n text_content = ""\n\n\nprint ("Content-type:text/html")\nprint ()\nprint("<!DOCTYPE html>")\nprint ("<html>")\nprint ("<head>")\nprint("<meta charset='UTF-8'></meta>")\nprint ("</head>")\nprint ("<body>")\nprint ("<form>")\nprint ("text_area:<br />")\nprint ("<textarea name='textcontent' rows='5' cols='20'></textarea>")\nprint ("<br />")\nprint ("<input type='submit' value='submit form' />")\nprint ("</form>")\nprint("<p>")\nprint(text_content) \nprint("</p>")\nprint ("</body>")\nprint ("</html>")\nRun Code Online (Sandbox Code Playgroud)\n这种方式使用UTF-8,当我尝试写一些东西时,它看起来像这样(写入textarea并提交):
\n\xc4\x8d\xc3\xadtam -> \xef\xbf\xbd\xef\xbf\xbdtam\nRun Code Online (Sandbox Code Playgroud)\n当我在 html 部分使用 latin-1 作为 python 编码和 utf-8 作为字符集时,它的工作原理如下:
\n\xc4\x8dasa -> \xc4\x8dasa (correctly)\nRun Code Online (Sandbox Code Playgroud)\n但对于带有重音符号的字符(例如 \xc3\xa1no),它会返回错误: …