py檔案增加encoding='utf-8',errors='ignore' 後仍然沒解決UnicodeDecodeError: 'gbk' codec can't decode byte
感覺和oracle的輸出字符集有關,gbk,gb18030都試了沒解決,注意紅色部分,修改後解決問題
#!/usr/bin/env python
import sys
import csv
import cx_Oracle
import codecs
import os
#os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.ZHS16GBK'
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.AL32UTF8'
conn = cx_Oracle.connect("lzsi", "lzsi", "10.1.1.210/orcl")
curs = conn .cursor()
tabname=sys.argv[1]
csv_file_dest = "/home/hadoop/oracledata/"+tabname+ ".csv"
outputFile = open(csv_file_dest,'w',encoding='utf-8',errors='ignore')# 'wb'
output = csv.writer(outputFile, dialect='excel')
sql = "select * from "+tabname #+" where rownum<100" # get a list of all tables
curs.execute(sql)
cols = []
for col in curs.description:
cols.append(col[0])
output.writerow(cols)
for row_data in curs: # add table rows
output.writerow(row_data)
outputFile.close()
curs.close()
conn.close()