Python 读取文件中的汉字并将其发送到浏览器
我正在尝试制作一个程序:Python 读取文件中的汉字并将其发送到浏览器,python,http,unicode,cjk,Python,Http,Unicode,Cjk,我正在尝试制作一个程序: 从文件中读取汉字列表,然后根据这些汉字制作字典(将符号与其含义关联起来) 拾取一个随机字符,并在收到GET请求时使用BaseHTTPServer模块将其发送到浏览器 一旦我能够正确地阅读和存储这些标志(我试着将它们写入另一个文件,以检查它们是否正确,并且是否有效),我就不知道如何将它们发送到我的浏览器 我连接到127.0.0.1:4321,我能做的最好的事情就是得到一个(据推测)url编码的汉字及其翻译 代码: 这就给了我: UnicodeEncodeError:“
- 从文件中读取汉字列表,然后根据这些汉字制作字典(将符号与其含义关联起来)
- 拾取一个随机字符,并在收到GET请求时使用
模块将其发送到浏览器BaseHTTPServer
Sign Translation
一 One
二 Two
三 Three
四 Four
五 Five
六 Six
七 Seven
八 Eight
九 Nine
十 Ten
因此,我的问题是:“如何让脚本中的汉字在浏览器中正确显示”通过编写元标记声明页面的编码,并确保以UTF-8编码整个Unicode字符串:
self.wfile.write(u'''\
<html>
<headers>
<meta http-equiv="content-type" content="text/html;charset=UTF-8">
</headers>
<body>
{} : {}
</body>
</html>'''.format(rKey,db[rKey]).encode('utf8'))
通过编写元标记声明页面的编码,并确保以UTF-8编码整个Unicode字符串:
self.wfile.write(u'''\
<html>
<headers>
<meta http-equiv="content-type" content="text/html;charset=UTF-8">
</headers>
<body>
{} : {}
</body>
</html>'''.format(rKey,db[rKey]).encode('utf8'))
self.wfile.write(u'''\
<html>
<headers>
<meta http-equiv="content-type" content="text/html;charset=UTF-8">
</headers>
<body>
{} : {}
</body>
</html>'''.format(rKey,db[rKey]).encode('utf8'))
self.send_response(200)
self.send_header('Content-Type','text/html; charset=utf-8')
self.end_headers()