看板 Python 關於我們 聯絡資訊
如題,假設程式碼是: test.py print('含有简体字的中文') 在 Windows 上直接執行 test.py 沒問題, 但如果在執行 test.py > test.log 就會發生錯誤: UnicodeEncodeError: 'cp950' codec can't encode character '\u7b80' in position 2: illegal multibyte sequence 請教各位大大,該如何調整程式碼, 才能做到直接執行能看到正常輸出, 且重導向 stdout 至檔案時也能正確寫入呢? -- 《終結內容農場》瀏覽器套件 Firefox: https://mzl.la/2g0I0ry (桌機 & Android 手機) Chrome: http://bit.ly/2wDWWyP 真相1: https://i.imgur.com/JKPzD1L.png
真相2: https://i.imgur.com/WTHmuVJ.png
-- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.243.226.168 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1516285270.A.B10.html ※ 編輯: danny0838 (111.243.226.168), 01/18/2018 23:05:57
uranusjr: 設環境變數 PYTHONIOENCODING="utf-8" 01/18 23:50
uranusjr: (程式碼不用動, 是執行環境要調整) 01/18 23:50