久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

在終端啟動(dòng)Python時(shí)報(bào)錯(cuò)的解決方案

瀏覽:28日期:2022-07-04 16:12:20

最近,在終端啟動(dòng)Python時(shí),報(bào)了一個(gè)錯(cuò)誤:

Failed calling sys.__interactivehook__Traceback (most recent call last): File 'd:ProgramDataAnaconda3libsite.py', line 439, in register_readline readline.read_history_file(history) File 'd:ProgramDataAnaconda3libsite-packagespyreadlinerlmain.py', line 165, in read_history_file self.mode._history.read_history_file(filename) File 'd:ProgramDataAnaconda3libsite-packagespyreadlinelineeditorhistory.py', line 82, in read_history_file for line in open(filename, ’r’):UnicodeDecodeError: ’gbk’ codec can’t decode byte 0xa7 in position 2167: illegal multibyte sequence

原因是Python的終端歷史文件中包含中文,但不能正確使用gbk解碼。查看了Python歷史文件(系統(tǒng)用戶目錄下的.python_history),其編碼方式為“utf-8”,而”history.py”中讀取歷史文件時(shí)使用的編碼方式為“gbk”,所以會報(bào)錯(cuò)。

解決方法

在history.py中使用`for line in open(filename, ’r’)`來打開文件并讀取每一行,使用的是默認(rèn)的編碼方式。需要根據(jù)不同文件的編碼方式傳入相應(yīng)的參數(shù)值。

1. 首先檢測出要打開的文件的編碼方式。

在類中定義一個(gè)私有方法_get_encoding,作用是檢測文件的編碼方式,并返回。(需要導(dǎo)入chardet包)

def _get_encoding(self, filename=None): if filename is None: return with open(filename, ’rb’) as f: return chardet.detect(f.read())[’encoding’]

2. 修改歷史文件內(nèi)容的讀取

encoding = self._get_encoding(filename) for line in open(filename, ’r’, encoding=encoding):self.add_history(lineobj.ReadLineTextBuff(ensure_unicode(line.rstrip())))

以上就是在終端啟動(dòng)Python時(shí)報(bào)錯(cuò)的解決方案的詳細(xì)內(nèi)容,更多關(guān)于終端啟動(dòng)python報(bào)錯(cuò)的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 吉水县| 济源市| 巴楚县| 彭阳县| 河西区| 弋阳县| 桑植县| 高安市| 长阳| 巴中市| 连山| 安庆市| 依兰县| 策勒县| 广元市| 牙克石市| 迁安市| 乐至县| 山东| 云浮市| 新平| 资中县| 思茅市| 天津市| 当阳市| 昌宁县| 阿拉善左旗| 高阳县| 山丹县| 望奎县| 开化县| 新野县| 边坝县| 西安市| 荥经县| 临桂县| 禹城市| 婺源县| 曲松县| 吴桥县| 三台县|