天天看点

python中遇到的问题及解决方法_使用python出现的问题及解决方案

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()的时候,使用不当会报错:

主要要考虑的情况如下:

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

正确形式:

import pickle

with open(filePath, "rb") as input_file:

content = pickle.load(input_file)

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

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

处理方法:

import pickle

with open(filePath, "rb") as input_file:

content = pickle.load(input_file, encoding="latin1")

由于操作系统不同:

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()

就会弹框,给出它的下载工具,下载即可

但是笔者在使用的时候,出现如下问题:

python中遇到的问题及解决方法_使用python出现的问题及解决方案

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()

成功