您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)怎么在Python中使用SQLite和Excel進行數(shù)據(jù)分析,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
環(huán)境配置
1、Python
2、Excel讀取庫
編寫代碼
代碼寫的很簡單,一個類,在構(gòu)造函數(shù)的時候初始化數(shù)據(jù)庫對象,析構(gòu)的時候釋放數(shù)據(jù)庫對象。一個插入數(shù)據(jù)函數(shù),一個讀取Excel函數(shù),話不多說,上代碼:
import sqlite3 import xlrd class FileDispose(object): """docstring for FileDispose""" def __init__(self, file): super(FileDispose, self).__init__() '''初始化數(shù)據(jù)庫實例''' self.conn = sqlite3.connect(file) self.cursor = self.conn.cursor() def __del__(self): '''釋放數(shù)據(jù)庫實例''' self.cursor.close() self.conn.close() '''數(shù)據(jù)庫插入操作''' def insert(self,id,name,sex,age,score,addr): sql = 'insert into student(id,name,sex,age,score,addr) values (%d,\"%s\",\"%s\",\"%s\",\"%s\",\"%s\")' % (int(id),name,sex,age,score,addr) print(sql) self.cursor.execute(sql) self.conn.commit() '''讀取Excel文件''' def readFile(self, file): data = xlrd.open_workbook(file) table = data.sheets()[2] for rowId in range(1, 100): row = table.row_values(rowId) if row: self.insert(rowId,row[0],row[1],row[2],row[3],row[4]) fd = FileDispose("F:/test.db") fd.readFile('F:/excel.xlsx')
數(shù)據(jù)庫表是我直接拿SQLiteSpy創(chuàng)建的,字段有id,name,sex,age,score,addr這幾個。
上述就是小編為大家分享的怎么在Python中使用SQLite和Excel進行數(shù)據(jù)分析了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。