1. 程式人生 > 實用技巧 >power shell下進入python後系統提示gbk錯誤,改編碼

power shell下進入python後系統提示gbk錯誤,改編碼

背景

  • power shell (CMD) 介面下,輸入python進入python環境時,系統提示gbk錯誤, 但能進入、使用
  • 提示錯誤來自於history file相關
    cmd 下進入 python 錯誤提示
  • win10, anaconda 4.8.3, python 3.8.3
  • 可能其它小程式更改了系統的編碼

辦法

  • 根據上述提示,找到history.py檔案, 備份一份
  • 找到錯誤行數,增加 encoding=‘utf-8’
  • 原來: for line in open(filename, ‘r’):
    更改後: for line in open(filename, ‘r’, encoding=‘utf-8’):