我正在清理 Europarl 的法语单语语料库(http://data.statmt.org/wmt19/translation-task/fr-de/monolingual/europarl-v7.fr.gz http://data.statmt.org/wmt19/translation-task/fr-de/monolingual/europarl-v7.fr.gz)。原始原始数据在.gz
文件(我使用下载wget
)。我想提取文本并查看它的外观,以便进一步处理语料库。
使用以下代码提取文本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))
第一行几行的打印结果如下:
类型(f_in) = 类 'gzip.GzipFile'
类型(文本)= 类“字节”
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 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
我尝试使用解码二进制数据utf8
and ascii
使用以下代码:
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))
它返回这样的错误:
UnicodeEncodeError:“ascii”编解码器无法对位置 26 中的字符“\xe9”进行编码:序号不在范围内(128)
我也尝试过使用codecs
and unicodedata
包打开文件,但它也返回编码错误。
您能否帮我解释一下我应该做什么才能以正确的格式获取法语文本,例如这样?
重开会议\n我宣布重开欧洲议会会议,并于 12 月 17 日开始对卖方进行插话,我们将重新开始,以敏锐的洞察力来度过美好的假期。\n祝您一切顺利,伟大的“ bogue de l'an 2000" 不是产品。\n
非常感谢您的帮助!