本文研究的主要是Python使用python/266708.html">pickle模塊存儲數據報錯解決方法,以代碼的形式展示,具體如下。
首先來了解下pickle模塊
接下來我們看下Python使用pickle模塊存儲數據報錯解決方法。
代碼:
# 寫入錯誤TypeError: write() argument must be str, not bytes# 讀取錯誤UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 0: illegal multibyte sequence
解決方案:
def storeTree(inputTree, fielname): import pickle # 寫文件時,注明 'wb' fw = open(fielname, 'wb') pickle.dump(inputTree, fw) fw.close()def grabTree(filename): import pickle # 讀文件時,注明 'rb' fr = open(filename, 'rb') fr = open(filename) return pickle.load(fr)storeTree(myTree, 'classifierStorage.txt')print(grabTree('classifierStorage.txt'))輸出:
{'no surfacing': {0: 'no', 1: {'flippers': {0: 'no', 1: 'yes'}}}}Process finished with exit code 0總結
以上就是本文關于Python使用pickle模塊存儲數據報錯解決示例代碼的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
新聞熱點
疑難解答