python中的一些报错记录(持续更新)

有时候代码报错,但是一下子又想不出报错的原因,这里慢慢记录下来,以防止忘记.

这里遇到一个问题,记一个问题

1、‘Nonetype' object is not iterable.

中文翻译:'Nonetype'对象是不可迭代的。

出现这种情况,可能是None的值被赋值给了多个对象如:

>>> a = b = None #没有报错
>>> a, b = None    #报错了
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not iterable
如果是函数,需要考虑函数并没有return,但是确赋值给了对象,或者赋值给了多个对象
 

2、TypeError:TypeError: list indices must be integers, not tuple

中文翻译:list indices必须是整数,而不是元组

这种情况我也不知道该如何解答,答题涉及到list是不是数组这个说法,在numpy中,提出的array中的数据类型必须全部相同,而list不是必须相同,list存放的是指针,是数据的地址.具体例子如下

>>> a = [[1,2],[2,3],[3,6]]
>>> a[1][1]
3
>>> a[1,1]#报错了
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list indices must be integers, not tuple
>>> b =np.array( [[1,2],[2,3],[3,6]])#而经过数组转化后,就不报错了
>>> b[1,1]
3
>>> b[1][1]
3
>>> 

3、'dict_keys' object does not support indexing

中文翻译:'dict_keys'对象不支持索引

这是我在生产决策树代码的时候遇到的,我的代码使用的是python3,但是源代码使用的是python2,所以报了这个错

firstStr = inputTree.keys()[0] 注意:python3会报错,需要改成

firstSides = list(inputTree.keys())
firstStr = firstSides[0]

4、ValueError: math domain error

中文翻译:数学域出错

这是我在用,math库的时候遇到的问题,先讲个简单例子

python 2 中 

>>> a = 1/3
>>> a
0


python3中

>>> a = 1/3
>>> a
0.3333333333333333

所以,在计算的时候注意一下,math.log(prob,2)这里的prob是不能为0的

>>> math.log(0,2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: math domain error

>>> math.sqrt(-1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: math domain error

这几个例子可以发现,比如,开根号下不能有负数,log的对象不能为0等

5、TypeError: can't multiply sequence by non-int of type 'numpy.float64'

can't multiply sequence by non-int of type 'float'

中文翻译:TypeError:不能将序列乘以非int

这个问题出现的很莫名奇妙,开始碰到时是在机器学习实战代码里遇到的,后来降numpy版本后又遇到第二个问题

weights = weights + alpha * error * dataArr[i]

网上查了一些问题,有说对numpy降级的,有说其他的,感觉不治本,于是我复现了一下这个问题,看下面

后来发现,只要np.array()一下就可以了,将这个序列变成矩阵就可以了

import numpy as np
>>> import numpy as np
>>> a = []
>>> a.append([1,2])
>>> a.append([2,4]) #append里面的是当成字符串,当然不能乘非int型啊
>>> a
[[1, 2], [2, 4]]
>>> a*1.0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'float'
>>> 1*a
[[1, 2], [2, 4]]
>>> a*2  #乘以整数的意思相当于复制粘贴,好了,懂了么?,只能乘以整数
[[1, 2], [2, 4], [1, 2], [2, 4]]

>>> b = np.array(a)
>>> b
array([[1, 2],
       [2, 4]])
>>> b*0.5
array([[0.5, 1. ],  #看,不报错了
       [1. , 2. ]])
>>>

6.exceeds limit of 89478485 pixels, could be decompression bomb DOS attack.

今天用python处理大图像的时候遇到

PIL对读取大图像的大小有个限制,这时候只需要扩充这个限制就可以了

在代码上加入

Image.MAX_IMAGE_PIXELS = 1000000000

7.'builtin_function_or_method' object is not subscriptable

检查中括号或者圆括号是否缺失,或者圆括号误写成中括号

8.TypeError: ‘odict_items’ object does not support indexing 或TypeError: ‘dict_items’

res = model.features._modules.items()[layer_index]
 res = list(model.features._modules.items())[layer_index]

9、使用tensorflow的object detection制作自己的tfrecord数据报的错

 Unicode strings with encoding declaration are not supported

原因:xml文件中有一行 <?xml version="1.0" encoding="UTF-8"?>, 所以是需要再编码的

增加一行   .encode("utf-8")

    xml_path = "../data/guangchang.xml"

    with tf.gfile.GFile(xml_path, 'r') as fid:
        xml_str = fid.read()


    xml = etree.fromstring(xml_str.encode("utf-8"))
    data = dataset_util.recursive_parse_xml_to_dict(xml)['annotation']
    print(data)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值