python错误总结

来源:互联网 发布:java内存落差较大 编辑:程序博客网 时间:2024/06/13 18:28

在这里记录一些Python遇到的小问题。


1、UnicodeDecodeError: 'utf-8' codec can't decode byte 0xce inposition 0: invalid continuation byte


解决方法:windows系统是用GBK编码的,将'utf-8'改成‘gbk'

具体编码格式修改:将txt文件用word打开,文件--选项--高级--web选项--编码--另存为纯文本--utf-8格式--确定--另存为--web选项--其他编码--utf-8—完成。

或者一开始就创建word文档,保存时选择纯文本,然后选择utf-8编码。

具体还要根据自己的代码分析。


2、IndentationError:unindent does not match any outer identation level


解决方法:在notepad++里视图修改对齐显示,以方便看是否对齐。Python中最好不使用空格,使用tab才能严格对齐。



3、AttributeError: 'str' object has no attribute 'decode'



这是参考一位博主的代码时运行出的问题,目的是识别英文单词后的中文,以在excel中分列。但是出现上述错误。

又参考了另外博主的文章,发现编码范围是:

GBK (GB2312/GB18030):

/x00-/xff  GBK双字节编码范围/x20-/x7f  ASCII/xa1-/xff 中文/x80-/xff 中文

UTF-8 (Unicode):
/u4e00-/u9fa5 (中文)/x3130-/x318F (韩文/xAC00-/xD7A3 (韩文)/u0800-/u4e00 (日文)

解决方法:

#hanzi_str = re.findall(r"([\x80-\xff]+)", line,re.MULTILINE) hanzi_str = re.findall(r"([\u4e00-\u9fa5]+)", line,re.MULTILINE)


4、TypeError: 'int' object is not callable

这个错误有一点莫名其妙,因为调用len函数时并没有将其赋值到名为len的变量,并在下文使用。

len('abcde')s = 'abcde'
l = len(s)

Spyder均报错如下:


在Python Shell下运行正常:


在JetBrain Pycharm下运行正常。

解决方法:关闭Spyder,重新打开。(就是这么任性,我还查了半天什么错)


原创粉丝点击