問題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 "<ipython-input-93-db5156b1d1f6>", line 1, in <module>
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)
<ipython-input-59-b72e2f1d5033> in <module>()
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.