今天使用Python連接數據庫,連接沒有問題,就是中文顯示亂碼,網上找了很多解決方案,
最后選擇使用這個
#!/usr/bin/env python # -*- coding:utf-8 -*- #Author: loovelj #date:2017/5/31 17:28 #-*- coding: utf-8 -*- #引用模塊cx_Oracle import cx_Oracle import sys import os reload(sys) sys.setdefaultencoding('utf8') os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.AL32UTF8'# 這個必須有,沒有回顯示 ?? #連接數據庫,參數參考cx_Oracle使用 conn = cx_Oracle.connect('sys/sys@localhost:1521/orcl') #獲取cursor cursor = conn.cursor() #使用cursor進行各種操作 cursor.execute('select * from test') result = cursor.fetchall() print (cursor.rowcount) i=0
title = [i[0] for i in cursor.description]
datas=pd.DataFrame(result,columns=title)
print datas
for row in result: while i<10: print row[2].encode('utf-8') #不能對一整行用這個函數,只能對單一值 i=i+1 #關閉cursor cursor.close() #關閉連接 conn.close()
