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模塊,缺啥裝啥...

sudo apt-get install python-wxtools

SystemError: cannot compile ‘Python.h’

沒法解析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 time

NameError: global name ‘datetime’ is not defined

解決方法: from datetime import datetime

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

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

ImportError: cannot import name 'Flask'

原因之一:當前路徑名取了一個“ flask ”(當前文件名為flask)

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

Python3以后刪除了has_key()方法!python2中可以。

解決方法:

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)

 

 

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM