Python 错误集锦

问题1:AttributeError: 'dict' object has no attribute 'iteritems'

AttributeError: 'dict' object has no attribute 'iteritems'

解决1:

Python3.x中:iteritems变为items

问题2:TypeError: 'range' object doesn't support item deletion

TypeError: 'range' object doesn't support item deletion

解决2:

#提示错误行
del(trainingSet[randIndex])
#检查trainingSet
trainingSet = range(2*minLen)
#修改为
trainingSet = list(range(2*minLen));

问题3:IndexError: list index out of range

IndexError: list index out of range

解决3:

1、范围问题

value无对应的index,造成越界

解决:增加临界判断语句,或者采取+-1

2、list为空,当list[0]会出现该错误

3、数据问题(有空行)

如下处理文件时,文件有空行

lenses=[inst.strip().split('\t') for inst in fr.readlines()]

解决:删除空行。

问题4:AttributeError: 'list' object has no attribute 'shape'

AttributeError: 'list' object has no attribute 'shape'

解决4:

list 转换为 matrix

#list转矩阵,矩阵行合并
x = [[1.1,2.1,3.1],[1.2,2.2,3.2],[1.3,2.3,3.3]] 
m = np.array(x)
print(m)
Out[1]: 
array([[ 1.1,  2.1,  3.1],
       [ 1.2,  2.2,  3.2],
       [ 1.3,  2.3,  3.3]])
#list转矩阵,矩阵列合并
x = [[1.1,2.1,3.1],[1.2,2.2,3.2],[1.3,2.3,3.3]] 
#将其转换为矩阵,表示三行三列,每一行表示一个点的信息
m = np.array(x).T
print(m)
Out[2]: 
array([[ 1.1,  1.2,  1.3],
       [ 2.1,  2.2,  2.3],
       [ 3.1,  3.2,  3.3]])

简单说:

a=([1,2,3,4]) #list

np.array(a)   #将a转化为numpy的array

a.tolist()    #将a转化为python的list

问题5: TypeError: %d format: a number is required, not list

TypeError: %d format: a number is required, not list

解决5:

#If you use the newer string formatting, there is no need to specify the type.
TypeError: float() argument must be a string or a number, not 'list'
# old
old_method = "%d,%d,%d" % (1, 2, 3)

# new (2.7)
new_method = "{},{},{}".format(1, 2, 3)

问题6:

Python3.6中比较数字和列表中的数字

解决6:

method1:

import operator
a = 5
b = list(range(5))
print(all([operator.gt(a, i) for i in b]))

结果:

True

method2:

a = 5

b = list(range(5))

print(a==b[5])
Traceback (most recent call last):

  File "", line 1, in 
    print(a==b[5])

IndexError: list index out of range

有错? 不要忘了 list from 0

print(a==b[4])
False

问题7 :IndentationError: expected an indented block

IndentationError: expected an indented block

解决7:

解释:IndentationError:预计有一个缩进块

查看缩进,即可解决。

问题8 :Python UnicodeDecodeError: 'gbk' codec can't decode byte 0xe9

Python文件读取报错

Python UnicodeDecodeError: 'gbk' codec can't decode byte 0xe9

解决8:

在使用Python 3.5版本编码的过程中,直接open(filename,’r’),总是报错:Python UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0xe9但是在用Python2.7版本的时候却什么错误都没有
解决方案:
在头文件中增加import codecs
然后将open(filename,’r’),改成:codecs.open(filename,’r’,encoding=’iso-8859-15’)问题得到解决

问题9:getHTMLText() takes 0 positional arguments but 1 was given

import requests
def getHTMLText(self):
    try:
        r = requests.get(url, timeout=30)
        r.raise_for_status()  #if status =!200 except
        r.encoding = "utf-8"  #encoding change to utf-8
        return r.text
    except:
        return ""
url = "http://www.zuihaodaxue.cn/zuihaodaxuepaiming2018.html"
print(getHTMLText(url))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
59-b72e2f1d5033> in ()
     12         return ""
     13 url = "http://www.zuihaodaxue.cn/zuihaodaxuepaiming2018.html"
---> 14 print(getHTMLText(url))

TypeError: getHTMLText() takes 0 positional arguments but 1 was given

解决9:

My getHTMLTextmethod needs ‘self’ as a parameter, since it is a class method and not a function. Adding that should make it work fine.

你可能感兴趣的:(python)