我正在使用word2vec模型在python中编写我的第一个应用程序。 这里是我的简单的代码在使用w2v时python中的编码问题

import gensim, logging

import sys

import warnings

from gensim.models import Word2Vec

logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)

def main():

####LOAD MODEL

model = Word2Vec.load_word2vec_format('models/vec-cbow.txt', binary=False)

model.similarity('man', 'women')

if __name__ == '__main__':

with warnings.catch_warnings():

warnings.simplefilter("error")

#warnings.simplefilter("ignore")

main()

我得到这个以下错误:

UnicodeDecodeError: 'utf8' codec can't decode bytes in position 96-97: invalid continuation byte

我试图通过增加这两条线解决它,但我仍然得到错误。

reload(sys) # Reload does the trick!

sys.setdefaultencoding('UTF8') #UTF8 #latin-1

该w2v模型是在英语句子训练。

编辑:以下是完整的堆栈:

**%run "...\getSimilarity.py"**

---------------------------------------------------------------------------

UnicodeDecodeError Traceback (most recent call last)

**...\getSimilarity.py in ()**

64 warnings.simplefilter("error")

65 #warnings.simplefilter("ignore")

---> 66 main()

**...\getSimilarity.py in main()**

30 ####LOAD MODEL

---> 31 model = Word2Vec.load_word2vec_format('models/vec-cbow.txt', binary=False) # C binary format

32 model.similarity('man', 'women')

**...\AppData\Local\Enthought\Canopy\User\lib\site-packages\gensim-0.12.4-py2.7-win-amd64.egg\gensim\models\word2vec.pyc in load_word2vec_format(cls, fname, fvocab, binary, encoding, unicode_errors)**

1090 else:

1091 for line_no, line in enumerate(fin):

-> 1092 parts = utils.to_unicode(line.rstrip(), encoding=encoding, errors=unicode_errors).split(" ")

1093 if len(parts) != vector_size + 1:

1094 raise ValueError("invalid vector on line %s (is this really the text format?)" % (line_no))

**...\AppData\Local\Enthought\Canopy\User\lib\site-packages\gensim-0.12.4-py2.7-win-amd64.egg\gensim\utils.pyc in any2unicode(text, encoding, errors)**

215 if isinstance(text, unicode):

216 return text

--> 217 return unicode(text, encoding, errors=errors)

218 to_unicode = any2unicode

219

**...\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.6.2.3262.win-x86_64\lib\encodings\utf_8.pyc in decode(input, errors)**

14

15 def decode(input, errors='strict'):

---> 16 return codecs.utf_8_decode(input, errors, True)

17

18 class IncrementalEncoder(codecs.IncrementalEncoder):

**UnicodeDecodeError: 'utf8' codec can't decode bytes in position 96-97: invalid continuation byte**

任何提示如何解决这个问题呢? 在此先感谢。

2017-01-02

sareem

+0

'models/vec-cbow.txt'有多大?可以通过文件共享网站将其包含在问题中?它似乎不是'utf-8'编码的。 –

+0

它是2.25 GB。我没有得到你的意思“可以通过文件共享网站将它包含在问题中?”? –

+0

不是太大了。没有意义。 –

python编码解码单词_在使用w2v时python中的编码问题相关推荐

  1. python新手入门教程思路-Python新手入门教程_教你怎么用Python做数据分析

    Python新手入门教程_教你怎么用Python做数据分析 跟大家讲了这么多期的Python教程,有小伙伴在学Python新手教程的时候说学Python比较复杂的地方就是资料太多了,比较复杂.很多网上 ...

  2. python以某种编码进行打印_如何在一场面试中展现你对Python的coding能力?

    (点击上方快速关注并设置为星标,一起学Python) 来源:Python数据科学    链接: https://mp.weixin.qq.com/s/KwjLpeRT4l-pOc_iRCLWzg 如果 ...

  3. python正则表达式查找单词_正则表达式查找特定单词之后的所有单词?

    我有一根绳子如下:Features: -Includes hanging accessories. -Artist: William-Adolphe Bouguereau. -Made with 10 ...

  4. python数据分析模块包括_数据开发必会 | Python数据分析模块

    作为数据开发,Python强大的数据分析模块还是必须要会的,横向拓展数据分析与挖掘技术栈也是很有必要的.本文将对Pandas.NumPy.SciPy.Matplotlib等分析挖掘库的安装和使用进行简 ...

  5. python代码编写工具_编写更好的Python代码的终极指南

    python代码编写工具 Despite its 尽管它 downsides, Python remains the king of today's programming world. Its ve ...

  6. 纯文本文件的字符编码未声明_浅谈几种常见的字符编码

    引子 相信大家平时工作和生活中,都遇到过乱码的现象.比如PDF格式的文件复制粘贴到Word文件中,很容易出现错误,影响对文件的识别.那么这种现象是如何产生的呢?下面就带大家一探究竟. ASCII 最早 ...

  7. 用python处理excel 数据分析_像Excel一样使用python进行数据分析(1)

    (虽然是转载,但是是我每块都测试过得,容易出问题的地方我会添加一些自己的经验,仅供参考) 摘要:本篇文章通过python与excel的功能对比介绍如何使用python通过函数式编程完成excel中的数 ...

  8. python打造excel神器_超简单:用Python让Excel飞起来

    前言 如何获取学习资源 章Python快速上手 1.1为什么要学习用Python控制Excel 1.2Python编程环境的搭建 1.2.1安装Python官方的编程环境IDLE 1.2.2安装与配置 ...

  9. python 调用bat失败_要想顺利通过Python面试,你最起码需要达到白银段位!

    近几年 Python 非常热门,在学术界和产业界的使用率显著提高.目前学习Python的人数日益增多,Python在近3年的编程语言受欢迎度中一直处于榜首.今天我们就来讲讲在产业界,需要具备哪些能力才 ...

最新文章

  1. 怎么安装linux系统 硬盘,如何实现硬盘安装linux系统
  2. pythonassertbug_还在 Bug 不断?不妨试试这 2 个装X技巧
  3. DSP集成开发工具CCS的Git工具使用说明(三)
  4. android 两个竖排listview,Android布局 – 在ListView行内垂直堆叠两个TextView
  5. 小TIPS:合拼gridview的header
  6. 项目中需要总结的内容
  7. java swing进度条_Java Swing创建自定义闪屏:在闪屏上添加Swing进度条控件(转)
  8. 我有文章了,但也不想搞学术了
  9. 从微信的改版中我看到了微信的焦虑
  10. oracle 定时任务例子【项目例子】
  11. NSA 分享植入 web shell 的常用漏洞列表
  12. Spring中的AOP(二)——AOP基本概念和Spring对AOP的支持
  13. readhat7mysql_Redhat7.3安装MySQL8.0.22的详细教程(二进制安装)
  14. Powershell下设置环境变量
  15. 高等数学公式(latex)
  16. win10计算机性能设置,巧设置让Win10运行更流畅
  17. 长春市职称计算机考试成绩查询,长春市助理工程师查询网站
  18. 知乎live-李笑来-人人都能用英语-笔记
  19. 「卷无人道」,手机app定制
  20. 【渝粤题库】广东开放大学 文化服务营销管理 形成性考核 (2)

热门文章

  1. 新手算法学习之路----二分法Last-position-of-Target
  2. iptables禁止端口和开放端口
  3. 汉军Hundre考勤数据库数据表分析总结
  4. XmlDocument类
  5. Python之异常追踪模块:traceback
  6. java中CardLayout的使用方法
  7. 尚未注册 OLE DB 访问接口 SQLNCLI10 7043 错误
  8. 花花酱leetcode 题目——搜索专题
  9. 拓扑排序两种实现方式
  10. 【Breadth-first Search 】934. Shortest Bridge