时间:2021-07-01 10:21:17 帮助过:4人阅读
# 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