pandas+sqlalchemy导入Excel数据到MySQL
excel 文件内容
prov:省份 city:城市
数据库结构
CREATE TABLE `big_data_area` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL COMMENT '省份或城市名称',
`type` int(1) NOT NULL COMMENT '类型 1:省份 2:城市',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='大数据省市数据';
程序
class ExportData(object):
def __init__(self):
self.DIALECT = 'mysql'
self.DRIVER = 'pymysql'
self.USERNAME = 'root'
self.PASSWORD = 'root'
self.HOST = '127.0.0.1'
self.PORT = 3306
self.DATABASE = 'test'
self.CHARSET = 'utf8'
self.SQLALCHEMY_DATABASE_URI = "{}+{}://{}:{}@{}:{}/{}?charset={}".format(self.DIALECT, self.DRIVER, self.USERNAME, self.PASSWORD, self.HOST, self.PORT, self.DATABASE, self.CHARSET)
self.SQLALCHEMY_TRACK_MODIFICATIONS = True
self.connect = create_engine(self.SQLALCHEMY_DATABASE_URI)
def export_excel(self):
df = pd.read_excel('C:\\Users\\Administrator\\Documents\\pro_city.xlsx', sheet_name='sheet1')
self.data_handler(df, 1)
self.data_handler(df, 2)
def data_handler(self, data, type_value):
if type_value == 1:
field = 'prov'
elif type_value == 2:
field = 'city'
else:
raise Exception("类型值错误")
df = data[[field]]
df = df.drop_duplicates([field])
df['type'] = type_value
df.columns = ['name', 'type']
print(df)
pd.io.sql.to_sql(df, 'big_data_area', self.connect, if_exists='append', index=False)
print("导入数据{}成功!".format(field))
if __name__ == '__main__':
exportData = ExportData()
exportData.export_excel()
运行结果
|