Python實(shí)現(xiàn)excel轉(zhuǎn)sqlite的方法
本文實(shí)例講述了Python實(shí)現(xiàn)excel轉(zhuǎn)sqlite的方法。分享給大家供大家參考,具體如下:
Python環(huán)境的安裝配置就不說了,個(gè)人喜歡pydev的開發(fā)環(huán)境。
python解析excel需要使用第三方的庫(kù),這里選擇使用xlrd
先看excel內(nèi)容:

然后是生成的數(shù)據(jù)庫(kù):

下面是源代碼:
#!/usr/bin/python
# encoding=utf-8
'''''
Created on 2013-4-2
@author: ting
'''
from xlrd import open_workbook
import sqlite3
import types
def read_excel(sheet):
# 判斷有效sheet
if sheet.nrows > 0 and sheet.ncols > 0:
for row in range(1, sheet.nrows):
row_data = []
for col in range(sheet.ncols):
data = sheet.cell(row, col).value
# excel表格內(nèi)容數(shù)據(jù)類型轉(zhuǎn)換 float->int,unicode->utf-8
if type(data) is types.UnicodeType: data = data.encode("utf-8")
elif type(data) is types.FloatType: data = int(data)
row_data.append(data)
check_data_length(row_data)
# 檢查row_data長(zhǎng)度
def check_data_length(row_data):
if len(row_data) == 3:
insert_sqlite(row_data)
def insert_sqlite(row_data):
# 打開數(shù)據(jù)庫(kù)(不存在時(shí)會(huì)創(chuàng)建數(shù)據(jù)庫(kù))
con = sqlite3.connect("test.db")
cur = con.cursor()
try:
cur.execute("create table if not exists contacts(_id integer primary key "\
"autoincrement,name text,age integer,number integer)")
# 插入數(shù)據(jù)不要使用拼接字符串的方式,容易收到sql注入攻擊
cur.execute("insert into contacts(name,age,number) values(?,?,?)", row_data)
con.commit()
except sqlite3.Error as e:
print "An error occurred: %s", e.args[0]
finally:
cur.close
con.close
xls_file = "test.xls"
book = open_workbook(xls_file)
for sheet in book.sheets():
read_excel(sheet)
print "------ Done ------"
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python常見數(shù)據(jù)庫(kù)操作技巧匯總》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- Python讀取excel指定列生成指定sql腳本的方法
- Python3讀取Excel數(shù)據(jù)存入MySQL的方法
- python3+mysql查詢數(shù)據(jù)并通過郵件群發(fā)excel附件
- Python使用SQLite和Excel操作進(jìn)行數(shù)據(jù)分析
- python實(shí)現(xiàn)讀取excel寫入mysql的小工具詳解
- Python實(shí)現(xiàn)讀寫sqlite3數(shù)據(jù)庫(kù)并將統(tǒng)計(jì)數(shù)據(jù)寫入Excel的方法示例
- Python實(shí)現(xiàn)將sqlite數(shù)據(jù)庫(kù)導(dǎo)出轉(zhuǎn)成Excel(xls)表的方法
- python 讀取excel文件生成sql文件實(shí)例詳解
- Python解析excel文件存入sqlite數(shù)據(jù)庫(kù)的方法
- 利用python在excel里面直接使用sql函數(shù)的方法
相關(guān)文章
python每次處理固定個(gè)數(shù)的字符的方法總結(jié)
使用python每次處理固定個(gè)數(shù)的字符,很多情況下都會(huì)遇到。本文對(duì)可能的方法做下總結(jié),供各位朋友學(xué)習(xí)參考2013-01-01
python筆記_將循環(huán)內(nèi)容在一行輸出的方法
今天小編就為大家分享一篇python筆記_將循環(huán)內(nèi)容在一行輸出的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08
使用Python統(tǒng)計(jì)代碼運(yùn)行時(shí)間的兩種方法
有時(shí)候我們需要記錄一個(gè)程序運(yùn)行的時(shí)間,下面這篇文章主要給大家介紹了關(guān)于使用Python統(tǒng)計(jì)代碼運(yùn)行時(shí)間的兩種方法,文中通過圖文以及示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
Python中Requests POST請(qǐng)求解讀
POST請(qǐng)求是HTTP協(xié)議中一種用于提交數(shù)據(jù)的方法,與GET請(qǐng)求獲取數(shù)據(jù)不同,POST常用于提交表單數(shù)據(jù)和上傳文件,本文介紹了如何使用Python的Requests包發(fā)送POST請(qǐng)求,包括基本的表單數(shù)據(jù)提交、發(fā)送JSON格式數(shù)據(jù),以及如何處理響應(yīng)狀態(tài)碼和錯(cuò)誤2024-11-11
Pyramid將models.py文件的內(nèi)容分布到多個(gè)文件的方法
默認(rèn)的Pyramid代碼結(jié)構(gòu)中,就只有一個(gè)models.py文件,在實(shí)際項(xiàng)目中,如果需要對(duì)models進(jìn)行分類,放到不同文件下,應(yīng)該怎么辦2013-11-11
詳解Python中sorted()和sort()的使用與區(qū)別
眾所周知,在Python中常用的排序函數(shù)為sorted()和sort()。本文將詳細(xì)介紹sorted()和sort()方法的代碼示例,并解釋兩者之間的區(qū)別,感興趣的可以了解一下2022-03-03

