python從Oracle讀取數(shù)據(jù)生成圖表
初次學(xué)習(xí)python,連接Oracle數(shù)據(jù)庫(kù),導(dǎo)出數(shù)據(jù)到Excel,再?gòu)腅xcel里面讀取數(shù)據(jù)進(jìn)行繪圖,生成png保存出來(lái)。
1、涉及到的python模塊(模塊安裝就不進(jìn)行解釋了):
import os import cx_Oracle import openpyxl import time import csv import xlrd from matplotlib import pyplot as plt from matplotlib import font_manager
2、連接數(shù)據(jù)庫(kù)
oracle客戶端要根據(jù)自己python對(duì)應(yīng)的版本進(jìn)行下載
import os
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8' #字符轉(zhuǎn)換,如果配置了環(huán)境變量這里可用不要
os.environ['TNS_ADMIN'] = 'D:\\Python\\instantclient_12_2' #oracle客戶端,如果配置了環(huán)境變量這里可用不要
os.environ['Path'] = 'D:\\Python\\instantclient_12_2' #自己電腦oracle客戶端所在位置
import cx_Oracle
# 方法一:用戶名、密碼和監(jiān)聽(tīng)分開(kāi)寫(xiě)
# conn=cx_Oracle.connect('username/password@host/orcl') #連接數(shù)據(jù)庫(kù)
# 方法二:用戶名、密碼和監(jiān)聽(tīng)寫(xiě)在一起
conn = cx_Oracle.connect('username/password@host:1521/ORCL') # utf-8顯示中文
#方法三:配置監(jiān)聽(tīng)并連接
# tns=cx_Oracle.makedsn('host',1521,'orcl',encoding = 'utf-8',nencoding = "UTF-8")
# conn=cx_Oracle.connect('username','password',tns)
curs=conn.cursor() #獲取cursor
printHeader = True # include column headers in each table output
sql="select b.name,sum(a.tot_qty) from m_retail a ,c_store b " \
"where a.c_store_id=b.id and a.billdate=20200923 group by b.name" #sql語(yǔ)句
x=curs.execute(sql) #使用cursor進(jìn)行各種操作
rows= x.fetchall()
for row in rows:
print(row)
3、創(chuàng)建Excel文件,讀取SQL文件,將數(shù)據(jù)寫(xiě)入Excel保存到本地
def export_excel(sql,fileName):
rr = curs.execute(sql)
rows = curs.fetchall()
#獲取字段名
title = [ i[0] for i in curs.description ]
#創(chuàng)建excel表
wb = openpyxl.Workbook()
ws = wb.active
#插入字段名到第一行
for c in range(len(title)):
ws.cell(1,c+1,value = title[c])
#寫(xiě)入查詢數(shù)據(jù)
for r in range(len(rows)):
for c in range(len(rows[r])):
if rows[r][c]: #值不為空時(shí)寫(xiě)入,空值不寫(xiě)入
ws.cell(r+2,c+1,value=str(rows[r][c])) #str()防止用科學(xué)計(jì)數(shù)法寫(xiě)入造成信息丟失
#
# #保存sql腳本
# ws1 = wb.create_sheet('sql')
# ws1.cell(1,1,value=sql)
#
wb.save(fileName)
wb.close()
curs.close()
if __name__ == '__main__':
# 方法一:用戶名、密碼和監(jiān)聽(tīng)分開(kāi)寫(xiě)
# conn=cx_Oracle.connect('username/password@host/orcl') #連接數(shù)據(jù)庫(kù)
# 方法二:用戶名、密碼和監(jiān)聽(tīng)寫(xiě)在一起
conn = cx_Oracle.connect('username/password@host:1521/ORCL') # utf-8顯示中文
#方法三:配置監(jiān)聽(tīng)并連接
# tns=cx_Oracle.makedsn('host',1521,'orcl',encoding = 'utf-8',nencoding = "UTF-8")
# conn=cx_Oracle.connect('username','password',tns)
curs= conn.cursor()
#打開(kāi)sql文件獲取sql語(yǔ)句
with open('零售查詢.sql',encoding= 'utf-8') as sql_0:#encoding= 'utf-8'
sql = sql_0.read()
# sql = "select b.name as 店倉(cāng),sum(a.tot_qty) as 數(shù)量 from m_retail a ,c_store b " \
# "where a.c_store_id=b.id and a.billdate=20201010 group by b.name"
cur_date = time.strftime("%Y-%m-%d", time.localtime())#"%Y-%m-%d-%H%M%S",取值日期
wjm='零售2'+cur_date+'.xlsx'
export_excel(sql,wjm)
conn.close()
4、打開(kāi)剛剛保存的Excel文件,且讀取需要的數(shù)據(jù)
# 打開(kāi)文件
data = xlrd.open_workbook(wjm)
cur_month = time.strftime("%m", time.localtime())#取值月份
# 查看工作表
# data.sheet_names()
# print("sheets:" + str(data.sheet_names()))
# 通過(guò)文件名獲得工作表,獲取工作表Sheet
# table = data.sheet_by_name(Sheet1)
table =data.sheet_by_index(0)#默認(rèn)讀取sheet1
cel_A1=table.cell(0,0)#取指定單元格的值
cel_B1=table.cell(0,1)
col_A=table.col_values(0,1)#取指定列的值
col_B=table.col_values(1,1)#
col_B = [ int(x) for x in col_B ]#文本轉(zhuǎn)數(shù)字
# print(cel_A1)
# print(col_B)
# print("整行值:" + str(table.row_values(0)))
# print("整列值:" + str(table.col_values(0,1)))
# print("整列值:" + str(table.col_values(1,1)))
# print(col_A)
5、繪制條形圖
#設(shè)置繪圖時(shí)的,中文字符顯示
#my_font=font_manager.FontProperties(fname=r"C:/Windows/Fonts/simhei.ttf", size=18)
plt.rcParams ['font.family']=['sans-serif']
plt.rcParams ['font.sans-serif']=['simhei' ]
plt.rcParams['font.size'] = '20'
plt.rcParams['xtick.labelsize']=16
plt.rcParams['ytick.labelsize']=16
#設(shè)置圖形大小
plt.figure(figsize=(20,8),dpi=80)
#繪制條形圖
plt.bar(range(len(col_A)),col_B)
#設(shè)置x軸
plt.xticks(range(len(col_A)),col_A)
plt.title(cur_month+"月份銷(xiāo)售分布") #添加標(biāo)題
plt.savefig("./sig_size3.png")#將圖形保存到工程目錄
plt.show()
結(jié)果展示

以上就是python從Oracle讀取數(shù)據(jù)生成圖表的詳細(xì)內(nèi)容,更多關(guān)于python 生成圖表的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python+OpenCV圖像處理——實(shí)現(xiàn)直線檢測(cè)
這篇文章主要介紹了Python+OpenCV如何實(shí)現(xiàn)直線檢測(cè),幫助大家更好的利用python處理圖片,感興趣的朋友可以了解下2020-10-10
Python基于jieba分詞實(shí)現(xiàn)snownlp情感分析
情感分析(sentiment analysis)是2018年公布的計(jì)算機(jī)科學(xué)技術(shù)名詞,它可以根據(jù)文本內(nèi)容判斷出所代表的含義是積極的還是負(fù)面的等。本文將通過(guò)jieba分詞實(shí)現(xiàn)snownlp情感分析,感興趣的可以了解一下2023-01-01
Python中Word文件自動(dòng)化操作小結(jié)
Python-docx是一個(gè)Python庫(kù),提供了對(duì)Microsoft?Word(.docx文件)的讀寫(xiě)和修改功能,本文主要介紹了如何使用Python-docx實(shí)現(xiàn)Word文件自動(dòng)化操作,需要的可以參考下2024-04-04
python誤差棒圖errorbar()函數(shù)實(shí)例解析
這篇文章主要介紹了python誤差棒圖errorbar()函數(shù)實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
詳解在Python中以絕對(duì)路徑或者相對(duì)路徑導(dǎo)入文件的方法
這篇文章主要介紹了詳解在Python中以絕對(duì)路徑或者相對(duì)路徑導(dǎo)入文件的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Python?庫(kù)?PySimpleGUI?制作自動(dòng)化辦公小軟件的方法
Python?在運(yùn)維和辦公自動(dòng)化中扮演著重要的角色,PySimpleGUI?是一款很棒的自動(dòng)化輔助模塊,讓你更輕松的實(shí)現(xiàn)日常任務(wù)的自動(dòng)化,下面通過(guò)本文給大家介紹下Python?庫(kù)?PySimpleGUI?制作自動(dòng)化辦公小軟件的過(guò)程,一起看看吧2021-12-12
Python機(jī)器學(xué)習(xí)中實(shí)現(xiàn)距離和相似性計(jì)算詳解
這篇文章主要為大家詳細(xì)介紹了Python機(jī)器學(xué)習(xí)中實(shí)現(xiàn)距離和相似性計(jì)算的方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-03-03
Python自動(dòng)化測(cè)試pytest中fixtureAPI簡(jiǎn)單說(shuō)明
這篇文章主要為大家介紹了Python自動(dòng)化測(cè)試pytest中fixtureAPI的簡(jiǎn)單說(shuō)明,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-10-10

