python中的常见错误.
TypeError: Object of type ‘int64’ is not JSON serializable
一般这是numpy的int类型无法被json化,所以需要将numpy的int转为原生类型.
1 | # pandas返回的 |
ValueError: unsupported format character ‘xx’ at index
1 | In [13]: s = "%jimo %s" % ('hehe') |
TypeError: ‘dict_items’ object is not subscriptable
在python3中dict.keys()或items()返回的是个迭代器,所以不能slice.但可以转成list再迭代:
1 | list(d.keys())[2:4] |
tuple parameter unpacking is not supported in python 3
python3中tuple不能当做lambda参数.
1 | points = [ (1,2), (2,3)] |
‘str’ object has no attribute ‘decode’
这又是个版本问题,在python2.x中字符串有decode方法,而python3没有,因为
python3默认把字符串当成unicode处理,而python2不是.
因此当视图在python2环境下比较中文字符串时:
1 | s == '寂寞' |
会出现警告:
1 | UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode |
解决办法也很简单,只要s是utf8的话,将’寂寞’进行解码即可:
1 | s == '寂寞'.decode('utf-8') |
而python3下则不需要.
Cannot uninstall ‘pyzmq’.
Cannot uninstall ‘pyzmq’. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.
这个是在使用pip安装jupyter时出现的错误,无法卸载,因为是作为系统项目进行安装的.
所以解决办法很简单,只需要在系统里把pyzmq模块卸载了先.