在python中处理unicode字符串
我使用的是基于英文维基百科的fasttext预训练模型。它按预期工作 但是当我用其他语言尝试相同的代码时,我得到了一个错误,如本页所示 该错误与unicode有关:在python中处理unicode字符串,python,python-unicode,unicode-escapes,Python,Python Unicode,Unicode Escapes,我使用的是基于英文维基百科的fasttext预训练模型。它按预期工作 但是当我用其他语言尝试相同的代码时,我得到了一个错误,如本页所示 该错误与unicode有关: UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 15: invalid start byte 我尝试使用原始二进制选项打开文件。我在load.py文件中更改了函数load\u words\u raw: with open(file_pat
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 15: invalid start byte
我尝试使用原始二进制选项打开文件。我在load.py文件中更改了函数load\u words\u raw:
with open(file_path, 'rb') as f:
现在我得到了一个不同的错误:
ValueError:无法将字符串转换为浮点:b'\x00l\x02'
我不知道如何处理这个问题。试试这个:
data : str
with open('crawl-D.txt' ,'r', encoding='utf8') as file:
data = file.read()
str
将整个文件包含为string
用
float()
解析float位15的字节0x80。有一种可能性,文件可能被编码为UTF-16。
试试这个:
with open(path, encoding='utf-16') as f:
// your logic
您应该将笔记本文件的第二行更改为:
#!wget https://dl.fbaipublicfiles.com/fasttext/vectors-crawl/cc.mr.300.vec.gz
因此,指向vec文件,而不是bin文件:
#!wget https://dl.fbaipublicfiles.com/fasttext/vectors-crawl/cc.mr.300.bin.gz
当你遇到这样的错误时,你应该把repr(s)
打印到控制台上,看看字符串s
到底包含了什么。你能把执行“head-n 5001 cc.mr.300.bin | tail-n 5000>/tmp/marathi.vec”的结果发布到我们可以下载的地方吗?好的,我刚刚比较了两个vec文件,marathi.vec文件包含二进制数据,无法直接处理,而english.vec文件是纯浮点数,表示为普通ASCII。