python faker 的学习

此次根据建表语句造数:

test_tab1 需要造100w数据

test_tab2 需要造1000w数据

create table test_tab1(
codeid numeric(10,0), 
prov varchar(20),    
city varchar(20),	
country varchar(20),
name varchar(6),  
birth timestamp,  
comm varchar(100),
index test_tab1_idx1(city,country),
index test_tab1_idx2(name),
index test_tab1_idx3(codeid)
);


create table test_tab2(
codeid numeric(10,0),   
account_id numeric(13,0), 
deal_dt timestamp,  
deal_type numeric(5,0), 
deal_money numeric(18,3),
charge_money numeric(18,3), 
lock_type numeric(1,0), 
expire_time timestamp,
comms varchar(10),  
index test_tab2_idx1(deal_dt),
index test_tab2_idx2(codeid)
);

借助python 的faker造数据,当然啦,数据要有一定的要求:

造数脚本

from faker import Factory

import random
import os
# 判断文件是否存在
t1 = "test_tab1.csv"
if (os.path.exists(t1)) :
	#存在,则删除文件
	os.remove(t1)
fake = Factory().create('zh_CN')
for i in range(1000000):
    codeid = random.randint(1, 10000)
    prov = fake.province()
    city = fake.city_name()
    country = fake.country()
    name = fake.name()
    birth = fake.date_object(2070-1-1)
    comm = fake.pystr(min_chars=10, max_chars=18)
    with open('%s'%t1, 'a', encoding='UTF-8') as fp:
        fp.write("{},{},{},{},{},{},{}".format(codeid,prov,city,country,name,birth,comm))
        fp.write('\n')
print('end')
fp.close()
from faker import Factory
import datetime
import random
import os

# 判断文件是否存在
t2 = "test_tab2.csv"
if (os.path.exists(t2)):
    # 存在,则删除文件
    os.remove(t2)
fake = Factory().create('zh_CN')


now_date = datetime.datetime.now()
data_int = int(now_date.strftime('%Y'))
startday = datetime.datetime(data_int, 1, 1)
endday = datetime.datetime(data_int, 12, 31)
diffstart  = startday.toordinal() - now_date.toordinal()
diffend = endday.toordinal() - now_date.toordinal()

for i in range(1,10000001):
    codeid = random.randint(1, 10000)
    account_id = i
    deal_dt = fake.date_between_dates(diffstart, diffend)
    deal_type = random.randint(1, 100)
    deal_money = random.randint(10000, 100000) + round(random.random(), 3)
    charge_money = random.randint(10000, 100000) + round(random.random(), 3)
    lock_type = random.randint(1, 9)
    expire_time = fake.date_between_dates(diffstart+366, diffend+366)
    comm = fake.pystr(min_chars=5, max_chars=10)
    with open('%s' % t2, 'a', encoding='UTF-8') as fp:
        fp.write("{},{},{},{},{},{},{},{},{}".format(codeid, account_id, deal_dt, deal_type, deal_money, charge_money,lock_type, expire_time, comm))
        fp.write('\n')
print('end')
fp.close()

参考:

Python Faker库常用操作 函数(自用)_ljh_2057的博客-CSDN博客

python随机生成库faker库api详解 - 寂静的天空 - 博客园 (cnblogs.com)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值