python问题:pickle.UnpicklingError: the STRING opcode argument must be quoted

pycharm无法正常引入自定义类

This inspection detects names that should resolve but don’t. Due to dynamic dispatch and duck typing, this is possible in a limited but useful number of cases. Top-level and class-level items are supported better than instance items.

这是因为,pycharm 是从source目录作为根目录进行查找的,如果想要正常引包
右键项目->Make Directory as ->Sources Root

pickle.UnpicklingError: the STRING opcode argument must be quoted

在使用pickle.load()的时候,使用不当会报错:
主要要考虑的情况如下:

1 打开文件时候是否是以“rb”形式打开:

正确形式:

import pickle
with open(filePath, "rb") as input_file:
  content = pickle.load(input_file)

2 是否是因为pickle文件的版本和使用的python版本不同:

也就是说,比如使用python2.7生成的pickle文件,想用python3.6来取出内容
处理方法:

import pickle
with open(filePath, "rb") as input_file:
  content = pickle.load(input_file, encoding="latin1")

3 由于操作系统不同:

Unix 的 “\n” 和 DOS 的 “\r\n”
处理方法:
重新生成一个新的文件:

original = "word_data.pkl"
destination = "word_data_unix.pkl"

content = ''
outsize = 0
with open(original, 'rb') as infile:
  content = infile.read()
with open(destination, 'wb') as output:
  for line in content.splitlines():
  outsize += len(line) + 1
  output.write(line + str.encode('\n'))

print("Done. Saved %s bytes." % (len(content)-outsize))

再使用 word_data_unix.pkl 就可以成功读取了

nltk.dowload()
nltk 是一个python自然语言处理中非常常用的包
但是使用的时候,如果这个语料库或者包,没有下载,它会提示下载
一般来说

import nltk
nltk.download()

就会弹框,给出它的下载工具,下载即可
但是笔者在使用的时候,出现如下问题:
ssl error
解决方案,不让SSL进行检查

import nltk
import ssl

try:
    _create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
    pass
else:
    ssl._create_default_https_context = _create_unverified_https_context

nltk.download()

成功

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值