在python中处理unicode字符串

问题描述 投票:1回答:3

我正在使用基于英语维基百科的fasttext预训练模型。它按预期工作......

https://github.com/shantanuo/pandas_examples/blob/master/nlp/fasttext_english.ipynb

但是当我用其他语言尝试相同的代码时,我收到一个错误,如本页所示......

https://github.com/shantanuo/pandas_examples/blob/master/nlp/fasttext_marathi.ipynb

该错误与unicode有关:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 15: invalid start byte

我试图使用Raw Binary选项打开文件。我更改了load.py文件中的load_words_raw函数:

with open(file_path, 'rb') as f:

现在我得到一个不同的错误:

ValueError:无法将字符串转换为float:b'\ x00l \ x02'

我不知道如何处理这个问题。

python python-unicode unicode-escapes
3个回答
1
投票

您应该将笔记本文件的第二行更改为:

#!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

1
投票

位置15的字节0x80。文件有可能以UTF-16编码。试试这个:

with open(path, encoding='utf-16') as f:
   // your logic   


0
投票

试试这个:

data : str
with open('crawl-D.txt' ,'r', encoding='utf8') as file:
    data = file.read()

str将整个文件包含为string

float解析float()

© www.soinside.com 2019 - 2024. All rights reserved.