


This article mainly introduces the method of converting excel to sqlite in Python, and analyzes the related operation skills of Python based on the third-party library xlrd to read Excel files and write to sqlite in the form of examples. Friends in need You can refer to the following
The example of this article describes the method of converting excel to sqlite in Python. Share it with everyone for your reference, the details are as follows:
I won’t talk about the installation and configuration of the Python environment. I personally like the pydev development environment.
Python needs to use a third-party library to parse excel. Here we choose to use xlrd
First look at the excel content:
Then it is generated Database:
The following is the source code:
#!/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表格内容数据类型转换 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长度 def check_data_length(row_data): if len(row_data) == 3: insert_sqlite(row_data) def insert_sqlite(row_data): # 打开数据库(不存在时会创建数据库) 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)") # 插入数据不要使用拼接字符串的方式,容易收到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 ------"
The above is the detailed content of Introduction to the method of converting excel to sqlite in Python. For more information, please follow other related articles on the PHP Chinese website!

如何使用PHP和SQLite创建用户登录系统在当今互联网时代,用户登录系统是许多网站和应用程序的基本功能之一。本文将介绍如何使用PHP和SQLite创建一个简单而强大的用户登录系统。SQLite是一个嵌入式数据库引擎,它是一个零配置的、服务器端的数据库引擎。PHP是一种流行的服务器端脚本语言,它与SQLite结合使用可以创建出灵活且高效的用户登录系统。通过以

使用PHP和SQLite实现用户权限和访问控制在现代的web应用程序中,用户权限和访问控制是非常重要的一部分。通过正确的权限管理,可以确保只有经过授权的用户能够访问特定的页面和功能。在本文中,我们将学习如何使用PHP和SQLite来实现基本的用户权限和访问控制。首先,我们需要创建一个SQLite数据库来存储用户和其权限的信息。下面是简单的用户表和权限表的结构

PHP和SQLite:如何进行数据压缩和加密在许多Web应用程序中,数据的安全性和存储空间的利用率是非常重要的考虑因素。PHP和SQLite是两个非常广泛使用的工具,本文将介绍如何使用它们来进行数据压缩和加密。SQLite是一种轻量级的嵌入式数据库引擎,它没有独立的服务器进程,而是直接与应用程序进行交互。PHP是一种流行的服务器端脚本语言,被广泛用于构建动态

随着互联网的发展,博客成为越来越多人分享自己生活、知识和想法的平台。如果你也想创建一个自己的博客,那么本文将介绍如何使用PHP和SQLite来创建一个简单的博客。确定需求在开始创建博客之前,我们需要确定自己想要实现的功能。例如:创建博客文章编辑博客文章删除博客文章显示博客文章列表显示博客文章详情用户认证和权限控制安装PHP和SQLite我们需要安装PHP和S

使用PHP和SQLite实现数据图表和可视化概述:随着大数据时代的到来,数据图表和可视化成为了展示和分析数据的重要方式。在本文中,将介绍如何使用PHP和SQLite实现数据图表和可视化的功能。以一个实例为例,展示如何从SQLite数据库中读取数据,并使用常见的数据图表库来展示数据。准备工作:首先,需要确保已经安装了PHP和SQLite数据库。如果没有安装,可

如何使用PHP和SQLite进行数据导入和导出导入和导出数据是在开发网站或应用程序时常见的任务之一。使用PHP和SQLite,我们可以轻松地将数据从外部文件导入到SQLite数据库中,并从数据库导出数据到外部文件。本文将介绍如何使用PHP和SQLite进行数据导入和导出,并提供相应的代码示例。数据导入首先,我们需要准备一个包含要导入的数据的外部文件。这个文件

PHP和SQLite:如何处理长连接和断线重连引言:在Web开发中,PHP和SQLite是两个常用的技术。然而,长连接和断线重连是在使用PHP和SQLite时经常遇到的一些问题。本文将介绍如何在PHP中处理长连接和断线重连的问题,并提供一些实例代码,以帮助开发者更好地理解和解决这些问题。一、长连接问题在使用PHP连接SQLite数据库时,长连接(Persis

如何使用PHP和SQLite进行全文搜索和索引策略引言:在现代的应用程序开发中,全文搜索功能在许多领域中都是不可或缺的。无论是在博客、新闻网站还是在电子商务平台上,用户都习惯使用关键字进行搜索。因此,为了提高用户体验并提供更好的搜索结果,我们需要使用适当的搜索和索引策略来提供全文搜索功能。在本文中,我们将探讨如何使用PHP和SQLite数据库来实现全文搜索和


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
