python項目實戰:簡單操做excle表的方法

前言python

Python操做Excle文件:使用xlwt庫將數據寫入Excel表格,使用xlrd 庫從Excel讀取數據。這篇文章主要介紹了python簡單操做excle的方法,Python操做Excle文件:使用xlwt庫將數據寫入Excel表格,使用xlrd 庫從Excel讀取數據。sql

從excle讀取數據存入數據庫數據庫

一、導入模塊:學習

import xlrdexcel

二、打開excle文件:索引

data = xlrd.open_workbook('excel.xls')it

三、獲取表、行/列值、行/列數、單元值table

獲取一個工做表:import

table = data.sheets()[0]    # 經過索引順序獲取循環

table = data.sheet_by_index(0)  # 經過索引順序獲取

table = data.sheet_by_name(u'Sheet1') # 經過名稱獲取

獲取整行/列的值,返回一個list,i表示行數和列數:

table.row_values(i)

table.col_values(i)

獲取總行/列數:

row_num = table.nrows

col_num = table.ncols

獲取單元格:

cell_value = table.cell(0,0).value

四、插入數據庫:獲取到一行的值後插入,循環每一行

row = table.nrows

print(row)

for i in range(1, row):

row_values = table.row_values(i)

ins_sql = 「insert into xxx(xx,xx,xx,xx) value(xx,xx,xx,xx)」

cursor.execute(ins_sql)

db.commit()

以上就是分享的內容,但願可以幫助到你們

學習歷來不是一我的的事情,要有個相互監督的夥伴,工做須要學習python或者有興趣學習python的夥伴能夠私信回覆小編「學習」 獲取資料,一塊兒學習

相關文章
相關標籤/搜索