用pandas从excel导入数据,到写入数据库, 综合来说其实很简单,只有pandas库中的两个函数:1.read_excel(),2.to_spl()。
首先先导入pandas库:
import pandas as pd
初始化参数:
def __init__(self):
self.path = os.path.join(settings.BASE_DIR, 'static/xls/basic.xls') # excel的存放路径
self.engine = create_engine('mysql+pymysql://test:test@localhost:3306/goldtwo?charset=utf8', encoding="utf-8") # 建立数据库连接
从excel中导入数据: 原文—>http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_excel.html?highlight=read_excel#pandas.read_excel
'''
pd.read_excel()
该函数主要的参数为io、sheetname、header、names、encoding。
io:excel文件,可以是文件路径、文件网址、file-like对象、xlrd workbook;
sheetname:返回指定的sheet,参数可以是字符串(sheet名