“文件"对象没有属性“rfind" [英] 'file' object has no attribute 'rfind'

查看:33
本文介绍了“文件"对象没有属性“rfind"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 word2vec 保存到文件中.

I am trying to save a word2vec to a file.

model = Word2Vec(sentences, size=vector_size, window=5, min_count=5, workers=4)
fo = open("foo.txt", "wb")
model.save(fo)

我在 genericpath.py 中收到以下错误

I am getting the following error in genericpath.py

File "word2Vec_impl.py", line 39, in <module>
model.save(fo, separately=None)
File "C:\Python27\lib\site-packages\gensim\models\word2vec.py", line 669, in s
ave
super(Word2Vec, self).save(*args, **kwargs)
File "C:\Python27\lib\site-packages\gensim\utils.py", line 269, in save
pickle(self, fname)
File "C:\Python27\lib\site-packages\gensim\utils.py", line 608, in pickle
with smart_open(fname, 'wb') as fout: # 'b' for binary, needed on Windows
File "C:\Python27\lib\site-packages\gensim\utils.py", line 596, in smart_open
_, ext = path.splitext(fname)
File "C:\Python27\lib\ntpath.py", line 190, in splitext
return genericpath._splitext(p, sep, altsep, extsep)
File "C:\Python27\lib\genericpath.py", line 91, in _splitext
sepIndex = p.rfind(sep)
AttributeError: 'file' object has no attribute 'rfind'

我哪里出错了?

推荐答案

Word2Vec.save 接受文件名,而不是文件对象.

Word2Vec.save accepts a filename, not a file object.

model = Word2Vec(sentences, size=vector_size, window=5, min_count=5, workers=4)
model.save('foo.txt')

这篇关于“文件"对象没有属性“rfind"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆