python cx_Oracle: UnicodeEncodeError: 'ascii' codec can't encode characters

wpgy發表於2019-10-11

使用 Python 連線到 Oracle 資料庫執行 INSERT 操作,因為資料中包含 Ã 之類的特殊字元,導致 execute SQL 語句報錯如下:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-2: ordinal not in range(128)

Python 版本:3.7
指令碼檔案編碼字符集:UTF-8

問題原因:
客戶端需要設定如下環境變數:
NLS_LANG=AMERICAN_AMERICA.AL32UTF8
LANG=AMERICAN_AMERICA.AL32UTF8


Linux 設定方法:
在 .bash_profile 新增如下兩行:
export NLS_LANG=AMERICAN_AMERICA.AL32UTF8
export LANG=AMERICAN_AMERICA.AL32UTF8

Windows 設定方法:
直接在環境變數裡面設定



來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31560527/viewspace-2659448/,如需轉載,請註明出處,否則將追究法律責任。

相關文章