求救使用cx_oracle獲取字符集爲US7ASCII的數據庫中的中文時亂碼

近期在使用cx_oracle獲取字符集爲US7ASCII的數據庫中的中文時亂碼

def getdata(lang):
    os.environ['NLS_LANG'] = lang
    userpwd = "ustest"
    dsn =cx_Oracle.makedsn("192.168.159.130",1521,sid="ustest")
    connection = cx_Oracle.connect("ustest",userpwd,dsn,encoding='US-ASCII',nencoding = "US-ASCII")
    cursor = connection.cursor()
    sql = """select name from students"""

    cursor.execute(sql)

    Datas=cursor.fetchall()
    name=Datas[-1][0]
    setp1 = name.encode('unicode_escape').decode('gb2312')
    print('setp1%s'%setp1)
    t = type(name)    
    print(Datas)
    print(name ,t)
    cursor.close()
    connection.close() 
if __name__ == "__main__":
    getdata(lang='AMERICAN_AMERICA.US7ASCII')
如果按上面的代碼執行時會報Datas=cursor.fetchall()
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe7 in position 0: ordinal not in range(128)

如果將encoding='US-ASCII',nencoding = "US-ASCII"改爲utf-8則中文亂碼
setp1\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd
[('???',), ('??',), ('3F3F',), ('������',)]
������ <class 'str'>

改成gbk,ISO-8859-1也一樣
這裏的nls_lang也與服務器端設置的一致。求大佬幫忙
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章