目录
错误1、TabError: inconsistent use of tabs and spaces in indentation
错误2:'ascii' codec can't encode characters in position 16-17: ordinal not in range(128)
错误3.SyntaxError: invalid character in identifier(标识符中的无效字符)
错误4:TypeError: 'NoneType' object is not iterable(TypeError:'NoneType'对象不可迭代)
错误1、TabError: inconsistent use of tabs and spaces in indentation
如图:
其中,45行老是报错:TabError: inconsistent use of tabs and spaces in indentation
解决办法:检查这个代码段(从41行开始),要不全部用tab,要不全部用4个空格进行缩进。
错误2:'ascii' codec can't encode characters in position 16-17: ordinal not in range(128)
解决办法:
方案1:在程序中加入以下代码:
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
方案2:在python的Lib(or lib)\site-packages文件夹下新建一个sitecustomize.py,内容为:
# encoding=utf-8
import sys
reload(sys)
sys.setdefaultencoding('utf-8)
此时重启python解释器,执行sys.getdefaultencoding(),发现编码已经被设置为utf-8的了,多次重启之后,效果相同,这是因为系统在python启动的时候,自行调用该文件,设置系统的默认编码,而不需要每次都手动的加上解决代码,属于一劳永逸的解决方法。
注意:我跑的是tf代码,所以在路径(~/anaconda2/envs/tensorflow/lib/python2.7/site-packages)中用命令
vim sitecustomize.py。
添加了sitecustomize.py。
错误3.SyntaxError: invalid character in identifier(标识符中的无效字符)
原因:可能代码行内夹杂中文的空格,tab等非文字字符.
解决办法:仔细检查,逐一排除。
错误4:TypeError: 'NoneType' object is not iterable(TypeError:'NoneType'对象不可迭代)
reference:https://www.cnblogs.com/zhaijiahui/p/8391701.html
原因:这个错误提示一般发生在将None赋给多个值时。
解决办法:应该考虑到else的情况,在if条件不满足时,函数默认返回None。
总结:
1. 将None赋给多个值时,会出现提示:TypeError: 'NoneType' object is not iterable
2. 函数返回值一定要考虑到条件分支的覆盖
3. 函数执行完毕且没有return语句时,python会自动return None
持续更新中。。。。。