Python中常见错误包括什么,如何应对处理
这篇文章主要介绍“Python中常见错误包括什么,如何应对处理”,有一些人在Python中常见错误包括什么,如何应对处理的问题上存在疑惑,接下来小编就给大家来介绍一下相关的内容,希望对大家解答有帮助,有这个方面学习需要的朋友就继续往下看吧。
以上就是关于“Python中常见错误包括什么,如何应对处理”的介绍了,感谢各位的阅读,如果大家想要了解更多相关的内容,欢迎关注博信,小编每天都会为大家更新不同的知识。
IndentationError: unexpected indent
Python 中强制缩进,, IndentationError: unexpected indent 缩进错误 这类错误非常常见,一般都是由于tab在不同的平台上占用长度不同导致,有些事程序员自己直接使用空格或其他来顶替tab。 解决办法非常简单,在所在平台上使用标准的tab进行缩进,就OK了。
UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 106: illegal multibyte sequence
编码错误,可以通过指定字符集解决 : encoding = “utf-8”
io.UnsupportedOperation: not readable
文件不可读,可能是文件打开模式不对
UnboundLocalError: local variable 'a' referenced before assignment
局部作用域引用错误,可能原因是 a变量为局部变量,未定义,不可修改
no module named wx
缺少wx模块,缺啥装啥...
sudo apt-get install python-wxtools
SystemError: cannot compile ‘Python.h’
没法解析Python的头文件,解决方法:
#先更新下源 sudo apt-get update #安装python-dev sudo apt-get install python-dev
NameError: name ‘xrange’ is not defined
python版本问题,不兼容,python3版本的换成range()函数就行了。
ameError: global name ‘time’ is not defined
解决方法:import time
NameError: global name ‘datetime’ is not defined
解决方法: from datetime import datetime
typeError: not all arguments converted during string formatting
TypeError: load() got an unexpected keyword argument 'delimiter'
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 33: invalid start byte
编码错误,基本是由中文引起的(中文路径、中文编码)
ImportError: cannot import name 'Flask'
原因之一:当前路径名取了一个“ flask ”(当前文件名为flask)
AttributeError: 'dict' object has no attribute 'has_key'
Python3以后删除了has_key()方法!python2中可以。
解决方法:
if adict.has_key(key1): #改为 if key1 in adict:
bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library?
TypeError: object of type 'map' has no len()
ZeroDivisionError: float division by zero
map函数后 返回<map object at 0x000001D8259F95F8>
map(function, iterable, ......)
Python 2.x 返回列表。
Python 3.x 返回迭代器。 只用将iterator 转换成 list 即可, 比如 list(map())
TypeError: 'int' object is not iterable
不能直接用int进行迭代
参考:https://segmentfault.com/q/1010000011234516,https://blog.csdn.net/yeizisn/article/details/53069775
报错代码:
list(map(frozenset, C1)) # 对每一个元素 frozenset
问题在于:map这个函数的第二个参数要求可以迭代,C1里面的元素也得可以迭代。C1这个列表的每个元素都是int,不可迭代,应该也是list才行;
http://www.runoob.com/python/python-func-map.html
解决代码:
C1.append([item]) #注意!!!item一定要加中括号,代表列表; 不然C1的元素是int,int是不可迭代的;执行list(map(frozenset, C1))会报错。
_tkinter.TclError: unknown option "-lable"
一般是参数的名称出现错误
TypeError: select_algorithm() takes 0 positional arguments but 1 was given
错误出现在tkinter,为combobox添加选择事件
解决方法: 为函数添加参数*args
def select_algorithm(*args): #为函数添加参数*args global algo_selected algo_selected = algorithm_combobox.get() print(algo_selected)View Code
以上就是关于“Python中常见错误包括什么,如何应对处理”的介绍了,感谢各位的阅读,如果大家想要了解更多相关的内容,欢迎关注博信,小编每天都会为大家更新不同的知识。
版权声明
本文仅代表作者观点,不代表博信信息网立场。
上一篇:python - 深度学习的最新进展!三行代码实现AI绘画! 下一篇:三板斧