简体   繁体   English

Python中的编码问题-使用UTF-8时,“ ascii”编解码器无法编码字符“ \\ xe3”

[英]Encoding problems in Python - 'ascii' codec can't encode character '\xe3' when using UTF-8

I've created a program to print out some html content. 我创建了一个程序来打印一些html内容。 My source file is in utf-8, the server's terminal is in utf-8, and I also use: 我的源文件在utf-8中,服务器的终端在utf-8中,并且我还使用了:

out = out.encode('utf8')

to make sure, the character chain is in utf8. 确保字符链在utf8中。 Despite all that, when I use some characters like "ã", "é" in the string out, I get: 尽管如此,当我在字符串中使用诸如“ã”,“é”之类的字符时,我得到:

UnicodeEncodeError: 'ascii' codec can't encode character '\xe3' in position 84: ordinal not in range(128)

It seems to me that the print after: 在我看来,打印后:

print("Content-Type: text/html; charset=utf-8 \n\n")

It's being forced to use ASCII encoding... But, I just don't know this would be the case. 它被迫使用ASCII编码...但是,我只是不知道会是这种情况。

I guess you should read the file as unicode object, that way you might not need to encode it. 我猜您应该将文件作为unicode对象读取,这样就可能不需要对其进行编码。

import codecs
file = codecs.open('file.html', 'w', 'utf-8')

Thanks a lot. 非常感谢。

Here it goes how I've solved the encoding problem in with Python 3.4.1: First I've inserted this line in the code to check the output encoding: 这说明了我如何使用Python 3.4.1解决编码问题:首先,我在代码中插入了以下行以检查输出编码:

print(sys.stdout.encoding)

And I saw that the output encoding was: 我看到了输出编码为:

ANSI_X3.4-1968 -

which stands for ASCII and doesn't support characters like 'ã', 'é', etc. 代表ASCII,不支持'ã','é'等字符。

so, I've deleted the previous line, and inserted theses ones here to change the standard output encoding with theses lines 因此,我删除了上一行,并在此处插入了这些代码,以更改这些代码行的标准输出编码

import codecs

if sys.stdout.encoding != 'UTF-8':
    sys.stdout = codecs.getwriter('utf-8')(sys.stdout.buffer, 'strict')
if sys.stderr.encoding != 'UTF-8':
    sys.stderr = codecs.getwriter('utf-8')(sys.stderr.buffer, 'strict')

Here is where I found the information: 这是我找到信息的地方:

http://www.macfreek.nl/memory/Encoding_of_Python_stdout http://www.macfreek.nl/memory/Encoding_of_Python_stdout

PS: everybody says it's not a good practice to change the default encoding. PS:每个人都说更改默认编码不是一个好习惯。 I really don't know about it. 我真的不知道 In my case it has worked fine for me, but I'm building a very small and simple webapp. 就我而言,它对我来说很好用,但是我正在构建一个非常小而简单的Web应用程序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UnicodeEncodeError: 'ascii' 编解码器在 UTF-8 语言环境中打印时无法编码字符 '\\xe9' - UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' when printing in UTF-8 locale Python-'charmap'编解码器无法编码字符'\\ xe3' - Python - 'charmap' codec can't encode character '\xe3' UnicodeEncodeError: 'ascii' codec can't encode character '\\xe9' - -when using urlib.request python3 - UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' - -when using urlib.request python3 UnicodeEncodeError:'ascii'编解码器无法编码字符u'\\ xe9' - UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' UnicodeEncodeError:'ascii'编解码器不能编码字符u'\\ xe4' - UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' 'ascii'编解码器无法编码字符u'\\ xe9' - 'ascii' codec can't encode character u'\xe9' UnicodeEncodeError:'ascii'编解码器不能编码字符u'\\ xe9' - UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' python'ascii'编解码器无法编码字符 - python 'ascii' codec can't encode character UnicodeEncodeError:即使将记录器格式化为使用 utf-8 编码,“charmap”编解码器也无法对字符进行编码 - UnicodeEncodeError: 'charmap' codec can't encode character even after formatting logger to use utf-8 encoding PyQt4 字符编码:“ascii”编解码器无法编码字符 - PyQt4 character encoding: 'ascii' codec can't encode character
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM