中文的 windows cmd 編碼預設是Big5(cp950) ,而 Python3 的預設程式碼編碼是utf-8 (cp65001),如果在輸出時產生 「UnicodeEncodeError: ‘cp950’ codec can’t encode character … …」的錯誤必須在程式開頭加上:
# -*- coding: utf-8 -*-
若程式會在console輸出,則使用
print(mytext.encode(sys.stdin.encoding, "replace").decode(sys.stdin.encoding))
若使用檔案輸出,則必須在open中指定編碼參數:
out_file = open("File.txt","w",encoding="utf-8")