python read us7ascii Oracle database Chinese characters garbled solution

Oracle Business Database character set used to obtain results for the us7ascii, python execute sql Chinese garbled, no matter how encode, decode codec, remain unresolved. Similar online few cases, tried several solutions, the last referring to one case on StackOverflow https://stackoverflow.com/questions/21336211/how-to-read-national-characters-127-from-us7ascii-oracle-using- ORAC-CX-Python , a solution formed about the comparison, recorded.

import pandas as pd
import matplotlib.pyplot as plt
from sqlalchemy import create_engine
import chardet
import binascii
#import os
#os.environ['NLS_LANG'] = 'AMERICAN_AMERICA.US7ASCII'
#db = cx_Oracle.connect('usr/pwd@host/orcl')
#curs = db.cursor()
sql = 'select rawtohex(utl_raw.cast_to_raw(name)) as name from index where patient_id = \'s1\' '
#sql = 'select name from index where patient_id = \'s1\' '
engine = create_engine('oracle://'usr/pwd@host/orcl')
df = pd.read_sql_query(sql,engine)
tmp = binascii.unhexlify(df['name'][0])
cn = tmp.decode('GBK')
print(cn)

A method of using rawtohex utl_raw.cast_to_raw in SQL character conversion, then decode the results of a query ( 'GBK').

utl_raw.cast_to_raw be used as a database character set access Solutions.

 

Attachment:

java access us7ascii character set of Oracle database Chinese garbled solution:

str = new String(str.getBytes("GBK"),"iso-8859-1")

If you have proven more concise possible options, please enlighten me!

Guess you like

Origin www.cnblogs.com/Aiolos/p/12336379.html