标签:list import ini 字符 create ali root int rom
# coding:utf-8 from sqlalchemy import create_engine class connet_databases: def __init__(self): ‘‘‘ # 初始化数据库连接,使用pymysql模块 # MySQL的用户:root, 密码:147369, 端口:3306,数据库:mydb ‘‘‘ _host = ‘39.108.131.88‘ _port = 3306 _databases = ‘san_jin_sq‘ # ‘produce‘ # _username = ‘wuzaipei‘ _password = ‘wuzaipei‘ self._connect = r‘mysql+pymysql://{username}:{password}@{host}:{port}/{databases}‘.format( username=_username, password=_password, host=_host, port=_port, databases=_databases) engine = create_engine(connet_databases()._connect, echo=True)
# coding:utf-8 import random # 随机生成n个字符串列表 def randomGenerateList(al,n=0): ‘‘‘ :param al: 字符串列表 [‘八戒‘,‘悟空‘,‘物镜‘,‘师父‘] :param n: n 为生成字符串列表的长度 :return: ‘‘‘ alist = list(al) return [random.choice(alist) for _ in range(n)] def dict_conversion(col,dict_list): ‘‘‘ :param col: 数据库字段 :param dict_list: 把所有字段列表添加在dict里面 :return: 合并成一个表 ‘‘‘ col_ = list(col) dict_list_ = list(dict_list) return dict(zip(col_,dict_list_))
import random import uuid from updateMsql.connectDatabases import engine from updateMsql.generateDemand import randomGenerateList,dict_conversion import pandas as pd col = [ ‘id‘,‘日期‘,‘品种‘,‘批号‘,‘销售数量‘, ‘销售金额‘] date = pd.date_range(‘2018-7-11‘, ‘2019-10-30‘,freq=‘1D‘) n_index = date.__len__() ID = [_ for _ in range(n_index)] field1 = randomGenerateList([‘三金片‘,‘西瓜霜润喉片‘,‘桂林西瓜霜(喷剂)‘,‘西瓜霜清咽含片‘],n_index) field2 = randomGenerateList([‘1001‘,‘1002‘,‘1003‘,‘1004‘,‘1005‘,‘1006‘],n_index) field3 = [random.randint(100,500) for i in range(n_index)] field4 = [random.randint(500,1000) for j in range(n_index)] data = pd.DataFrame(data=dict_conversion(col,[ID,date,field1,field2,field3,field4])) data.to_sql(‘销售情况分析‘,engine,if_exists=‘replace‘,index=False) print(data.head()) print("----插入成功-----")
python pandas 自动生成批量测试数据插入数据库 mysql
标签:list import ini 字符 create ali root int rom
原文地址:https://www.cnblogs.com/wuzaipei/p/11291428.html