python讀取xlsx檔案

2021-07-10 16:22:42 字數 1733 閱讀 9965

我是在win7下讀取的。 python版本是:3.5

import xlrd

import re

import sqlite3

def read_xlsx():

workbook = xlrd.open_workbook('e:\20160322.xlsx')

booksheet = workbook.sheet_by_name('sheet1')

p = list()

for row in range(booksheet.nrows):

row_data =

for col in range(booksheet.ncols):

cel = booksheet.cell(row, col)

val = cel.value

try:

val = cel.value

val = re.sub(r'\s+', '', val)

except:

pass

if type(val) == float:

val = int(val)

else:

val = str( val )

return p

def operat_sqlite(*data):

# print(type(data))

# print(data)

print(data[0])

try:

conn = sqlite3.connect('e:\list.db')

except:

print('open sqlite3 failed.')

return

else: #運算元據庫

c = conn.cursor()

for item in data:

for i in range(len(item)):

dldmv = item[i][1]

lddmv = item[i][3]

ldmcv = item[i][2]

fhsslxv = item[i][5]

xzqhv = item[i][6]

try:

#creat sql

c.execute("insert into roadkey (dldm, lddm, ldmc, fhsslx, xzqh) values (?, ?, ?, ?, ?)", (dldmv, lddmv, ldmcv, fhsslxv, xzqhv))

conn.commit()

except:

print('insert roadky failed ')

pass

print(i)

print(item[i])

conn.close()

return

if __name__ == '__main__':

data_list = list()

data_list = read_xlsx()

operat_sqlite(data_list)

1. **功能:讀取xlsx檔案內容,插入到sqlite3資料庫裡面。

2. 安裝xlrd: pip install xlrd。 

sqlite3已經有庫了。。不用安裝,直接使用。

參考**:python xlsx讀取

python 操作excel讀寫

python開發_sqlite3_完整

python讀取xlsx檔案

encoding utf 8 from openpyxl import load workbook workbook load workbook u xlsx 相對路徑,找到需要開啟的檔案位置 booksheet workbook.active 獲取當前活躍的 sheet,預設是第乙個 sheet ...

Python 讀取 xlsx檔案

讀取weibo.xlsx檔案 import pandas as pd df pd.read excel weibo.xlsx worksheet df.values for file in worksheet print file file 每一行 print file 0 每一行的第一列結果如下 ...

用python讀取xlsx檔案

首先安裝xlrd庫,安裝方法 pip install xlrd import xlrd 開啟excel wb xlrd.open workbook test user data.xlsx 按工作簿定位工作表 sh wb.sheet by name testuserlogin print sh.nro...