Python Spacy NLP - TypeError: 参数'string'类型错误(应为unicode,得到str)

5

当我尝试在Spacy中读取txt文件时,遇到以下错误:

TypeError: 参数'string'类型不正确(应为Unicode,而不是str)

以下是代码:

from __future__  import unicode_literals
import spacy
nlp= spacy.load('en')
doc_file = nlp(open("example.txt").read())

doc_file = nlp(open("example.txt").read().decode()) - CristiFati
当我添加解码时,出现以下错误UnicodeDecodeError: 'ascii'编解码器无法解码位置955中的字节0xc2:该序号不在128的范围内 - Madu Nishant
你解决了这个问题吗?我想你只需要声明 doc = '',然后正确地以UTF8读取文件:with codecs.open(fpath, 'r', encoding='utf-8') as fr: doc = fr.read() 然后 doc_file = nlp(doc) - Wiktor Stribiżew
1个回答

1

你应该使用 nlp = spacy.blank('en') 而不是 spacy.load('en')。

nlp= spacy.blank('en')
doc_file = nlp(open("example.txt").read())


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接