简体   繁体   English

UnicodeEncodeError: 'ascii' 编解码器在 UTF-8 语言环境中打印时无法编码字符 '\\xe9'

[英]UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' when printing in UTF-8 locale

I am cleaning the monolingual corpus of Europarl for French ( http://data.statmt.org/wmt19/translation-task/fr-de/monolingual/europarl-v7.fr.gz ).我正在清理 Europarl 的法语单语语料库 ( http://data.statmt.org/wmt19/translation-task/fr-de/monolingual/europarl-v7.fr.gz )。 The original raw data in .gz file (I downloaded using wget ). .gz文件中的原始原始数据(我使用wget下载)。 I want to extract the text and see how it looks like in order to further process the corpus.我想提取文本并查看它的外观,以便进一步处理语料库。

Using the following code to extract the text from gzip , I obtained data with the class being bytes .使用以下代码从gzip提取文本,我获得了类为bytes

with gzip.open(file_path, 'rb') as f_in:
    print('type(f_in)=', type(f_in))
    text = f_in.read()
    print('type(text)=', type(text))

The printed results for several first lines are as follows:几行的打印结果如下:

type(f_in) = class 'gzip.GzipFile' type(f_in) = class 'gzip.GzipFile'

type(text)= class 'bytes'类型(文本)= 类'字节'

b'Reprise de la session\\nJe d\\xc3\\xa9clare reprise la session du Parlement europ\\xc3\\xa9en qui avait \\xc3\\xa9t\\xc3\\xa9 interrompue le vendredi 17 d\\xc3\\xa9cembre dernier et je vous renouvelle tous mes vux en esp\\xc3\\xa9rant que vous avez pass\\xc3\\xa9 de bonnes vacances.\\nComme vous avez pu le constater, le grand "bogue de l\\'an 2000" ne s\\'est pas produit.\\n b'Reprise de la session\\nJe d\\xc3\\xa9clare rererise la session du Parlement europ\\xc3\\xa9en qui avait \\xc3\\xa9t\\xc3\\xa9 interrompue le vendredi 17 d\\xc3\\xa9cembre dernier et vexjeus en esp\\xc3\\xa9rant que vous avez pass\\xc3\\xa9 de bonnes vacances。\\nComme vous avez pu le constater, le Grand "bogue de l\\'an 2000" ne s\\'est pas produit。\\n

I tried to decode the binary data with utf8 and ascii with the following code:我尝试使用以下代码使用utf8ascii解码二进制数据:

with gzip.open(file_path, 'rb') as f_in:
    print('type(f_in)=', type(f_in))
    text = f_in.read().decode('utf8')
    print('type(text)=', type(text))

And it returned errors like this:它返回了这样的错误:

UnicodeEncodeError: 'ascii' codec can't encode character '\\xe9' in position 26: ordinal not in range(128) UnicodeEncodeError: 'ascii' 编解码器无法对位置 26 中的字符 '\\xe9' 进行编码:序号不在范围内 (128)

I also tried using codecs and unicodedata packages to open the file but it returned encoding error as well.我还尝试使用codecsunicodedata包来打开文件,但它也返回了编码错误。

Could you please help me explain what I should do to get the French text in the correct format like this for example?例如,你能帮我解释一下我应该怎么做才能以正确的格式获取法语文本吗?

Reprise de la session\\nJe déclare reprise la session du Parlement européen qui avait été interrompue le vendredi 17 décembre dernier et je vous renouvelle tous mes vux en espérant que vous avez passé de bonnes vacances.\\nComme vous avez pu le constater, le grand "bogue de l'an 2000" ne s'est pas produit.\\n Reprise de la session\\nJe déclare reprise la session du Parlement européen qui avait été interrompue le vendredi 17 décembre dernier et je vous renouvelle tous mes vux en espérant que vous avez passé de bonnes vacances, conpus bogue de l'an 2000" ne s'est pas produit。\\n

Thank you a ton for your help!非常感谢您的帮助!

Many thanks for all your help!非常感谢您的帮助! I found a simple solution to work around.我找到了一个简单的解决方案。 I'm not sure why it works but I think that maybe the .txt format is supported somehow?我不确定它为什么有效,但我认为.txt格式可能以某种方式受支持? If you know the mechanism, it would be extremely helpful to know.如果你知道这个机制,那么了解它会非常有帮助。

with gzip.open(file_path, 'rb') as f_in:
    text = f_in.read()

with open(os.path.join(out_dir, 'europarl.txt'), 'wb') as f_out:
    f_out.write(text)

When I print out the text file in terminal, it looks like this:当我在终端中打印出文本文件时,它看起来像这样:

Reprise de la session Je déclare reprise la session du Parlement européen qui avait été interrompue le vendredi 17 décembre dernier et je vous renouvelle tous mes vux en espérant que vous avez passé de bonnes vacances. Reprise de la session Je déclare reprise la session du Parlement européen qui avait été interrompue le vendredi 17 décembre dernier et je vous renouvelle tous mes vux en espérant que vous avez passé de bonnes vacances。 Comme vous avez pu le constater, le grand "bogue de l'an 2000" ne s'est pas produit. Comme vous avez pu le constater, le grand "bogue de l'an 2000" ne s'est pas produit。 En revanche, les citoyens d'un certain nombre de nos pays ont été victimes de catastrophes naturelles qui ont vraiment été terribles. En revanche, les citoyens d'un sure nombre de nos pays ontétévicores de catastrophes naturelles qui ont vraiment été farers。 Vous avez souhaité un débat à ce sujet dans les prochains jours, au cours de cette période de session. Vous avez souhaité un débat à ce sujet dans les prochains jours, au cours de cette periode de session。

The UnicodeEncodeError is occurring because when printing, Python encodes strings to bytes, but in this case, the encoding being used - ASCII - has no character that matches '\\xe9', so the error is raised.发生 UnicodeEncodeError 是因为在打印时,Python 将字符串编码为字节,但在这种情况下,所使用的编码 - ASCII - 没有与 '\\xe9' 匹配的字符,因此会引发错误。

Setting the PYTHONIOENCODING environment variable forces Python to use a different encoding - the value of the environment variable.设置PYTHONIOENCODING环境变量会强制 Python 使用不同的编码 - 环境变量的值。 The UTF-8 encoding can encode any character, so calling the program like this solves the issue: UTF-8 编码可以编码任何字符,所以像这样调用程序可以解决问题:

PYTHONIOENCODING=UTF-8 python3  europarl_extractor.py

assuming the code is something like this:假设代码是这样的:

import gzip

if __name__ == '__main__':
    with gzip.open('europarl-v7.fr.gz', 'rb') as f_in:
        bs = f_in.read()
        txt = bs.decode('utf-8')
        print(txt[:100])

The environment variable may be set in other ways - via an export statement, in .bashrc , .profile etc.环境变量可以通过其他方式设置 - 通过export语句,在.bashrc.profile等中。

An interesting question is why Python is trying to encode output as ASCII.一个有趣的问题是为什么Python 试图将输出编码为 ASCII。 I had assumed that on *nix systems, Python essentially looked at the $LANG environment variable to determine the encoding to use.我假设在 *nix 系统上,Python 本质上是查看$LANG环境变量来确定要使用的编码。 But in the case the value of $LANG is fr_FR.UTF-8 , and yet Python is using ASCII as the output encoding.但是在$LANG的值是fr_FR.UTF-8的情况下,Python 使用 ASCII 作为输出编码。

From looking at the source for the locale module, and this FAQ , these environment variables are checked, in order:通过查看locale模块的源代码和此FAQ ,按顺序检查这些环境变量:

'LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE'

So it may be that one of LC_ALL or LC_CTYPE has been set to a value that mandates ASCII encoding in your environment (you can check by running the locale command in your terminal; also running locale charmap will tell you the encoding itself).因此,可能LC_ALLLC_CTYPE已设置为要求在您的环境中进行 ASCII 编码的值(您可以通过在终端中运行locale命令来检查;同时运行locale charmap会告诉您编码本身)。

暂无
暂无

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

相关问题 UnicodeEncodeError:'ascii'编解码器无法编码字符u'\\ xe9' - UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' UnicodeEncodeError:'ascii'编解码器不能编码字符u'\\ xe9' - UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' 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' codec can't encode character u'\\xe9' in position 54: ordinal not in range(128) - UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 54: ordinal not in range(128) 'ascii'编解码器无法编码字符u'\\ xe9' - 'ascii' codec can't encode character u'\xe9' Python eyed3 UnicodeEncodeError:'ascii'编解码器无法在位置17编码字符u'\\ xe9':序数不在范围内(128) - Python eyed3 UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 17: ordinal not in range(128) UnicodeEncodeError:'ascii'编解码器无法在位置31编码字符'\\ xe9':安装金字塔期间序数不在range(128)中 - UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 31: ordinal not in range(128) during installing pyramid Python中的编码问题-使用UTF-8时,“ ascii”编解码器无法编码字符“ \\ xe3” - Encoding problems in Python - 'ascii' codec can't encode character '\xe3' when using UTF-8 UnicodeEncodeError:'ascii'编解码器不能编码字符u'\\ xe4' - UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' Python - 'ascii' 编解码器无法对位置 5 中的字符 u'\\xe9' 进行编码:序号不在范围内(128) - Python - 'ascii' codec can't encode character u'\xe9' in position 5: ordinal not in range(128)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM