Github Link

需求

  现有2000+文件夹,每个文件夹下有若干excel文件,现在要将这些excel文件中的数据导入mysql。

  每个excel文件的第一行是无效数据。

  除了excel文件中已有的数据,还要添加一列,名为“at_company”,值为821。

流程

  (1)获取excel文件列表,并根据excel文件名确定之后需要创建的table名;

  (2)连接mysql

  (3)创建table

  (4)插入数据

  (5)断开连接

依赖模块

1. xlrd # to read excel files
2. mysql-connector-python # to work with Mysql

源代码

 #-*- coding: utf-8 -*-
import os,sys,datetime
import mysql.connector
import xlrd '''
the main function to import data
username: username of mysql database
password: password for username
database: a specific database in mysql
datapath: the absolute path or relative path of data folder
'''
def importDataHelper(username, password, database, datapath):
'''import data helper'''
'''
Step 0: Validate input database parameters
'''
try:
conn = mysql.connector.connect(user=username, password=password, database=database, use_unicode=True)
except mysql.connector.errors.ProgrammingError as e:
print e
return -1
'''
Step 1: Traverse files in datapath, store file paths and corresponding table names in lists
lists[0] is the list of files paths
lists[1] is the list of table names
'''
lists = getFilesList(datapath)
nfiles = len(lists[0])
'''
Step 2: Store data in mysql via a for-loop
'''
cursor = conn.cursor()
for file_idx in xrange(0, nfiles):
file_path = lists[0][file_idx]
print "processing file(%d/%d):[ %s ]"%(file_idx+1, nfiles, file_path)
table_name = lists[1][file_idx]
num = storeData(file_path, table_name, cursor)
if num >= 0:
print "[ %d ] data have been stored in TABLE:[ %s ]"%(num, table_name)
conn.commit()
cursor.close()
'''
Step 3: Close connection
'''
conn.close() '''
get files list in the dir, including the files in its sub-folders
the return list contain two elements, the first element is a file names list
and the second element is a table names list(will be used for creating tables in database),
'''
def getFilesList(dir):
path_list = []
table_list = []
file_name_list = os.listdir(dir)
for file_name in file_name_list:
path = os.path.join(dir, file_name)
if os.path.isdir(path):
'''get the files in sub folder recursively'''
tmp_lists = getFilesList(path)
path_list.extend(tmp_lists[0])
table_list.extend(tmp_lists[1])
else:
path_list.append(path)
'''convert file name to mysql table name'''
file_name = file_name.split('.')[0] #remove .xls
# file_name = file_name.split('from')[0] #remove characters after 'from'
file_name = file_name.strip()#remove redundant space at both ends
file_name = file_name.replace(' ','_') #replace ' ' with '_'
file_name = file_name.replace('-','_') #replace ' ' with '_'
file_name = file_name.lower() #convert all characters to lowercase
table_list.append(file_name)
return [path_list, table_list] '''
store the data of file file_path in table table_name
file_path: file location
table_name: name of the table that will be created in database
cursor: a mysql cursor
'''
def storeData(file_path, table_name, cursor):
ret = 0
'''open an excel file'''
file = xlrd.open_workbook(file_path)
'''get the first sheet'''
sheet = file.sheet_by_index(0)
'''get the number of rows and columns'''
nrows = sheet.nrows
ncols = sheet.ncols
'''get column names'''
col_names = []
for i in range(0, ncols):
title = sheet.cell(1, i).value
title = title.strip()
title = title.replace(' ','_')
title = title.lower()
col_names.append(title)
'''create table in mysql'''
sql = 'create table '\
+table_name+' (' \
+'id int NOT NULL AUTO_INCREMENT PRIMARY KEY, ' \
+'at_company varchar(10) DEFAULT \'821\', ' for i in range(0, ncols):
sql = sql + col_names[i] + ' varchar(150)'
if i != ncols-1:
sql += ','
sql = sql + ')'
try:
cursor.execute(sql)
except mysql.connector.errors.ProgrammingError as e:
print e
# return -1 '''insert data'''
#construct sql statement
sql = 'insert into '+table_name+'('
for i in range(0, ncols-1):
sql = sql + col_names[i] + ', '
sql = sql + col_names[ncols-1]
sql += ') values ('
sql = sql + '%s,'*(ncols-1)
sql += '%s)'
#get parameters
parameter_list = []
for row in xrange(2, nrows):
for col in range(0, ncols):
cell_type = sheet.cell_type(row, col)
cell_value = sheet.cell_value(row, col)
if cell_type == xlrd.XL_CELL_DATE:
dt_tuple = xlrd.xldate_as_tuple(cell_value, file.datemode)
meta_data = str(datetime.datetime(*dt_tuple))
else:
meta_data = sheet.cell(row, col).value
parameter_list.append(meta_data)
# cursor.execute(sql, parameter_list)
try:
cursor.execute(sql, parameter_list)
parameter_list = []
ret += 1
except mysql.connector.errors.ProgrammingError as e:
print e
# return -1
return ret if __name__ == "__main__":
if len(sys.argv)<5:
print "Missing Parameters"
sys.exit()
elif len(sys.argv)>5:
print "Too Many Parameters"
sys.exit()
username = sys.argv[1]
password = sys.argv[2]
database = sys.argv[3]
datapath = sys.argv[4]
importDataHelper(username, password, database, datapath)

Readme文件(帮导师做的,所以用英文写的文档)

There are two dependency modules need to be installed.
1. xlrd # to read excel files
2. mysql-connector-python # to work with Mysql Directory Structure:
data_path: test files
ImportDataProgram.py: the main program Procedure:
(1) Get all the paths and names of the files need to be stored
(2) Connect MySQL
(3) Create tables for each file
(4) Insert data into each table Usage:
0. create a new database in mysql
For example, after logging in mysql in terminal, you can use the the following command
"create database test_database" to create a database named 'test_database',
you can replace "test_database" with any other names you like. 1. set username, password, database(created in step 0) and datapath in the tail of ImportDataProgram.py
2. run ImportDataProgram.py with the following command
python ImportDataProgram.py [username] [password] [database] [datapath]
# username: your username in your mysql
# password: the corresponding password
# database: the database you specific
# datapath: the directory of excel files
e.g.
python ImportDataProgram.py root root test_database data_path PS:
(1) The Length of Data In Table
I am not sure the maximum length of data, so I set the
length of data in mysql tables is 150 characters (you can find
the code in function storeData(file_path, table_name, cursor), the code is
" sql = sql + col_names[i] + ' varchar(150)' "), you can adjust it according
to your requirements.
(2)Table Name:
You can set the rules of table name, the code is following the comment code:
'''convert file name to mysql table name''' in function getFilesList(dir).

遇到的坑以及填坑方法:

(1)Python中argv参数用法

(2)Python使用xlrd读取excel文件的方法[1(比较简要)][2(比较详细)

(3)Python使用xlrd读取excel文件中日期类型变为浮点数[stack overflow][2中文博客

(4)Python遍历目录下的文件[1

(5)Python连接MySQL[1

(6)Python中print格式化输出(%),取消默认换行(,)[print用法]

(7)Python字符串连接[字符串操作]

(8)Python连接list[连接list]

(9)Python字符串替换[字符串替换]

[Python]将Excel文件中的数据导入MySQL的更多相关文章

  1. 将Excel表中的数据导入MySQL数据库

    原文地址: http://fanjiajia.cn/2018/09/26/%E5%B0%86Excel%E8%A1%A8%E4%B8%AD%E7%9A%84%E6%95%B0%E6%8D%AE%E5% ...

  2. 将excel文件中的数据导入到mysql

    ·在你的表格中增加一列,利用excel的公式自动生成sql语句,具体方法如下:          1)增加一列(假设是D列)          2)在第一行的D列,就是D1中输入公式:=CONCATE ...

  3. 用Python的pandas框架操作Excel文件中的数据教程

    用Python的pandas框架操作Excel文件中的数据教程 本文的目的,是向您展示如何使用pandas 来执行一些常见的Excel任务.有些例子比较琐碎,但我觉得展示这些简单的东西与那些你可以在其 ...

  4. 小技巧之“将Text文件中的数据导入到Excel中,这里空格为分割符为例”

    1.使用场景 将数据以文本导出后,想录入到Excel中,的简便方案, 起因:对于Excel的导出,Text导出明显会更方便些 2.将Text文件中的数据导入到Excel中,这里空格为分割符为例的步骤 ...

  5. Java读取、写入、处理Excel文件中的数据(转载)

    原文链接 在日常工作中,我们常常会进行文件读写操作,除去我们最常用的纯文本文件读写,更多时候我们需要对Excel中的数据进行读取操作,本文将介绍Excel读写的常用方法,希望对大家学习Java读写Ex ...

  6. python读取excel表格中的数据

    使用python语言实现Excel 表格中的数据读取,需要用到xlrd.py模块,实现程序如下: import xlrd #导入xlrd模块 class ExcelData(): def __init ...

  7. 使用Python从PDF文件中提取数据

    前言 数据是数据科学中任何分析的关键,大多数分析中最常用的数据集类型是存储在逗号分隔值(csv)表中的干净数据.然而,由于可移植文档格式(pdf)文件是最常用的文件格式之一,因此每个数据科学家都应该了 ...

  8. 将CSV文件中的数据导入到SQL Server 数据库中

    导入数据时,需要注意 CSV 文件中的数据是否包含逗号以及双引号,存在时,导入会失败 选择数据库 -> 右键 -> 任务 -> 导入数据 ,然后根据弹出的导入导出向导(如下图)中的提 ...

  9. 【Python】从文件中读取数据

    从文件中读取数据 1.1 读取整个文件 要读取文件,需要一个包含几行文本的文件(文件PI_DESC.txt与file_reader.py在同一目录下) PI_DESC.txt 3.1415926535 ...

随机推荐

  1. delphi 取得任意程序的命令行

    program GetCommandLineExDemo; uses Windows; constSystemHandleInformation = 16;ProcessBasicInformatio ...

  2. iOS 字符串 中包含 % 百分号的方法

    百分号的转换,NSString中需要格式化的字符串中百分号使用%%表示,而char*中百分号也是使用%%表示. 例如:NSLog(@"%%%@%%",@"hello&qu ...

  3. Android 用户登录界面

    本篇博客主要给大家演示怎样一步一步地创建一个类似于下图展示的这么一个UI界面: 一.准备图片资源 记住:因为Demo其中用到的图片资源都是直接从上面图片截取的,所以图片质量上面会差一些,只是.不影响我 ...

  4. UnityEngine.Time类属性解析

    UnityEngine.Time类包含的属性: public static int captureFramerate { get; set; } public static float deltaTi ...

  5. SharePoint 修改项目的new图标显示天数

    前言 最近有这么个需求,用户需要修改新建项目前面的new图标的显示天数,查了很久,发现有powershell命令或者stsadm命令可以,分享给大家. PowerShell命令 $wa = Get-S ...

  6. [rrdtool]监控和自己主动绘图,简单的监控.md

    如今想要监控服务的流量和并发数,但是又没那么多时间来写系统.其它的运维系统又不熟悉,于是就用现有的rrdtool shell做了个简单的监控界面,暂时用下,也算是个小实验把. rrdtool也是刚接触 ...

  7. 关于MySQL的行转列的简单应用

    sql 脚本 -- 创建表 学生表 CREATE TABLE `student` ( `stuid` VARCHAR(16) NOT NULL COMMENT '学号', `stunm` VARCHA ...

  8. 启明星手机版安卓android会议室预定系统 V1.0发布

    启明星手机版会议室预定系统 V1.0发布 在手机里输入 http://www.dotnetcms.org/e4.apk 或者扫描二维码下载 用户打开系统,可以实时查看所有会议室状态 点击会议室名称,可 ...

  9. HashMap 与 ConcurrentHashMap

    1. HashMap 1) 并发问题 HashMap的并发问题源于多线程访问HashMap时, 如果存在修改Map的结构的操作(增删, 不包括修改), 则有可能会发生并发问题, 表现就是get()操作 ...

  10. 奇怪吸引子---Aizawa

    奇怪吸引子是混沌学的重要组成理论,用于演化过程的终极状态,具有如下特征:终极性.稳定性.吸引性.吸引子是一个数学概念,描写运动的收敛类型.它是指这样的一个集合,当时间趋于无穷大时,在任何一个有界集上出 ...