python 常见报错汇总

python官方文档:https://docs.python.org/zh-cn/3/tutorial/index.html

1.indentationerror:unindent does not match any outer indentation level

原因:没有对齐导致的,设置notepad:视图--》显示符号--》显示空格和制表符 

参考:https://www.crifan.com/python_syntax_error_indentationerror/comment-page-1/

2.indentationerror expected indented block

原因:没有对齐导致的

3.UnicodeDecodeError:'utf-8' codec can't decode byte xxx in position

一般这种情况出现得较多是在国标码(GBK)和utf8之间选择出现了问题,出现异常报错是由于设置了decode()方法的第二个参数errors为严格(strict)形式造成的,因为默认就是这个参数,将其更改为ignore等即可:

  line.decode('utf-8','ignore')

4.TypeError: write() argument must be str, not bytes

文件打开方式有问题,用open("testfile.txt", "wb+")

5.urllib have no urlopen

python 用的时urllib.request.urlopen,所以要import urllib.request

6.TypeError: cannot use a string pattern on a bytes-like object

需要使用html_url=html_url.decode('utf-8')#python3  ;参考:https://blog.csdn.net/lxh199603/article/details/53192883

7.UnicodeDecodeError: 'gbk' codec can't decode byte 0xa1 in position 58: illegal multibyte sequence

解决办法1:

                FILE_OBJECT= open('order.log','r', encoding='UTF-8')

解决方法2:

       FILE_OBJECT= open('order.log','rb')
原文地址:https://www.cnblogs.com/zndxall/p/9203918.html