Python 中常見錯誤總結

IndentationError: unexpected indent 

Python 中強制縮進,,  IndentationError: unexpected indent   縮進錯誤

這類錯誤很是常見,通常都是因爲tab在不一樣的平臺上佔用長度不一樣致使,有些事程序員本身直接使用空格或其餘來頂替tab。 
解決辦法很是簡單,在所在平臺上使用標準的tab進行縮進,就OK了。

UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 106: illegal multibyte sequence

編碼錯誤,能夠經過指定字符集解決 : encoding = 「utf-8」

io.UnsupportedOperation: not readable

 文件不可讀,多是文件打開模式不對

UnboundLocalError: local variable 'a' referenced before assignment

局部做用域引用錯誤,可能緣由是 a變量爲局部變量,未定義,不可修改

no module named wx

缺乏wx模塊,缺啥裝啥...html

sudo apt-get install python-wxtools

SystemError: cannot compile ‘Python.h’

無法解析Python的頭文件,解決方法:python

#先更新下源
sudo apt-get update

#安裝python-dev
sudo apt-get install python-dev 

NameError: name ‘xrange’ is not defined

python版本問題,不兼容,python3版本的換成range()函數就好了。程序員

ameError: global name ‘time’ is not defined

解決方法:import timeflask

NameError: global name ‘datetime’ is not defined

解決方法: from datetime import datetimesegmentfault

typeError: not all arguments converted during string formatting

 

TypeError: load() got an unexpected keyword argument 'delimiter'

 

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 33: invalid start byte

 編碼錯誤,基本是由中文引發的(中文路徑、中文編碼)app

ImportError: cannot import name 'Flask'

緣由之一:當前路徑名取了一個「 flask 」(當前文件名爲flask)ide

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

Python3之後刪除了has_key()方法!python2中能夠。函數

解決方法:ui

if adict.has_key(key1):  
#改成
if key1 in adict:  

 bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library?

 

TypeError: object of type 'map' has no len()

 

ZeroDivisionError: float division by zero

 

map函數後 返回<map object at 0x000001D8259F95F8>

map(function, iterable, ......)

Python 2.x 返回列表。編碼

Python 3.x 返回迭代器。 只用將iterator 轉換成 list 便可, 好比  list(map()) 

TypeError: 'int' object is not iterable

不能直接用int進行迭代

參考:https://segmentfault.com/q/1010000011234516https://blog.csdn.net/yeizisn/article/details/53069775

報錯代碼:

list(map(frozenset, C1)) # 對每個元素 frozenset

問題在於:map這個函數的第二個參數要求能夠迭代,C1裏面的元素也得能夠迭代。C1這個列表的每一個元素都是int,不可迭代,應該也是list才行;

http://www.runoob.com/python/python-func-map.html

解決代碼:

C1.append([item])  #注意!!!item必定要加中括號,表明列表; 否則C1的元素是int,int是不可迭代的;執行list(map(frozenset, C1))會報錯。

 _tkinter.TclError: unknown option "-lable"

通常是參數的名稱出現錯誤

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

錯誤出如今tkinter,爲combobox添加選擇事件

解決方法: 爲函數添加參數*args

def select_algorithm(*args):   #爲函數添加參數*args
    global  algo_selected
    algo_selected = algorithm_combobox.get()
    print(algo_selected)
View Code

 ModuleNotFoundError: No module named 'cPickle'

緣由:python2有cPickle,可是在python3下,是沒有cPickle的;

解決辦法:將cPickle改成pickle便可

TypeError: getOpenFileName(parent: QWidget = None, caption: object = '', directory: object = '', filter: object = '', options: QFileDialog.Options = 0): argument 1 has unexpected type 'str'

# argument 1 是指第一個參數
# 它的意思是第一個參數不該該是str,因此查一下這個函數的幾個參數就行了
# 實際上是由於缺乏第一個參數

filename = QFileDialog.getOpenFileName(None, 'Open File','/')  #第三個參數是默認打開路徑,若是爲空則打開當前路徑

No module named 'sklearn.lda'

#  from sklearn.lda import LDA 這是sklearn0.16的寫法,以後的版本沒有了lda  能夠查一下sklearn各個版本的API
#  參考連接: https://stackoverflow.com/questions/46775155/no-module-named-sklearn-lda
#  爲了代碼的最少更改,能夠以下解決:

from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA

ValueError: too many values to unpack

#   參考連接:http://leonzhan.iteye.com/blog/1720315
#   上述連接中說:這種錯誤是指一個tuple值賦給一個tuple變量時,變量個數不夠形成的。如:
#   a, b = (1, 2, 3)

#   個人錯誤代碼:
    X, y = FileOpener.load_file(filename)
#   這裏的問題是: load_file返回了三個值 X, y, dataset, 因此再加一個值來接收,改成以下代碼:
    X, y,dataset = FileOpener.load_file(filename)

 

 

未完待續...................................................................................................................................................................................................................................................................................

相關文章
相關標籤/搜索