您现在的位置是:首页 >学无止境 >DATAFAKER 使用方法记录网站首页学无止境

DATAFAKER 使用方法记录

wowocpp 2023-07-01 08:00:02
简介DATAFAKER 使用方法记录

DATAFAKER 使用方法记录

win10 64位
Python 3.10.11

参考网址

datafaker的使用–详细教程
https://blog.csdn.net/A15517340610/article/details/105623103

https://github.com/gangly/datafaker

python 版本

It is compatible with python2.7 and python3.4+
也就是说 他兼容 Python2.7和 python3.4 以上的版本,不一定非要 python3.4

安装流程

pip install datafaker

C:Usersjack>pip install datafaker
Collecting datafaker
  Downloading datafaker-0.7.6.tar.gz (18 kB)
  Preparing metadata (setup.py) ... done
Collecting sqlparse==0.3.0
  Downloading sqlparse-0.3.0-py2.py3-none-any.whl (39 kB)
Collecting faker==2.0.2
  Downloading Faker-2.0.2-py2.py3-none-any.whl (879 kB)
     ---------------------------------------- 879.8/879.8 kB 704.7 kB/s eta 0:00:00
Collecting configparser
  Downloading configparser-5.3.0-py3-none-any.whl (19 kB)
Collecting sqlalchemy==1.3.8
  Downloading SQLAlchemy-1.3.8.tar.gz (5.9 MB)
     ---------------------------------------- 5.9/5.9 MB 1.1 MB/s eta 0:00:00
  Preparing metadata (setup.py) ... done
Collecting pywin32
  Downloading pywin32-306-cp310-cp310-win_amd64.whl (9.2 MB)
     ---------------------------------------- 9.2/9.2 MB 1.1 MB/s eta 0:00:00
Collecting python-dateutil>=2.4
  Downloading python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)
     ---------------------------------------- 247.7/247.7 kB 1.2 MB/s eta 0:00:00
Collecting six>=1.10
  Downloading six-1.16.0-py2.py3-none-any.whl (11 kB)
Collecting text-unidecode==1.3
  Downloading text_unidecode-1.3-py2.py3-none-any.whl (78 kB)
     ---------------------------------------- 78.2/78.2 kB 1.4 MB/s eta 0:00:00
Installing collected packages: text-unidecode, pywin32, sqlparse, sqlalchemy, six, configparser, python-dateutil, faker, datafaker
  WARNING: The script sqlformat.exe is installed in 'C:UsersjackAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310Scripts' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  DEPRECATION: sqlalchemy is being installed using the legacy 'setup.py install' method, because it does not have a 'pyproject.toml' and the 'wheel' package is not installed. pip 23.1 will enforce this behaviour change. A possible replacement is to enable the '--use-pep517' option. Discussion can be found at https://github.com/pypa/pip/issues/8559
  Running setup.py install for sqlalchemy ... done
  WARNING: The script faker.exe is installed in 'C:UsersjackAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310Scripts' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  DEPRECATION: datafaker is being installed using the legacy 'setup.py install' method, because it does not have a 'pyproject.toml' and the 'wheel' package is not installed. pip 23.1 will enforce this behaviour change. A possible replacement is to enable the '--use-pep517' option. Discussion can be found at https://github.com/pypa/pip/issues/8559
  Running setup.py install for datafaker ... done
Successfully installed configparser-5.3.0 datafaker-0.7.6 faker-2.0.2 python-dateutil-2.8.2 pywin32-306 six-1.16.0 sqlalchemy-1.3.8 sqlparse-0.3.0 text-unidecode-1.3

[notice] A new release of pip is available: 23.0.1 -> 23.1
[notice] To update, run: C:UsersjackAppDataLocalMicrosoftWindowsAppsPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0python.exe -m pip install --upgrade pip

C:Usersjack>

在这里插入图片描述
pip install mysqlclient

C:Usersjack>pip install mysqlclient
Collecting mysqlclient
  Downloading mysqlclient-2.1.1-cp310-cp310-win_amd64.whl (178 kB)
     ---------------------------------------- 178.4/178.4 kB 489.0 kB/s eta 0:00:00
Installing collected packages: mysqlclient
Successfully installed mysqlclient-2.1.1

[notice] A new release of pip is available: 23.0.1 -> 23.1
[notice] To update, run: C:UsersjackAppDataLocalMicrosoftWindowsAppsPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0python.exe -m pip install --upgrade pip

测试方法

navicat 新建 数据库
test_datafaker
在这里插入图片描述

create table stu (
	id int unsigned auto_increment primary key COMMENT '自增id',
	name varchar(20) not null comment '学生名字',
	school varchar(20) not null comment '学校名字',
	nickname varchar(20) not null comment '学生小名',
	age int not null comment '学生年龄',
	class_num int not null comment '班级人数',
	score decimal(4,2) not null comment '成绩',
	phone bigint not null comment '电话号码',
	email varchar(64) comment '家庭网络邮箱',
	ip varchar(32) comment 'IP地址',
	address text comment '家庭地址'
) engine=InnoDB default charset=utf8;

在这里插入图片描述
在这里插入图片描述
meta.txt内容:

id||int||自增id[:inc(id,1)]
name||varchar(20)||学生名字
school||varchar(20)||学校名字[:enum(file://names.txt)]
nickname||varchar(20)||学生小名[:enum(鬼泣, 高小王子, 歌神, 逗比)]
age||int||学生年龄[:age]
class_num||int||班级人数[:int(10, 100)]
score||decimal(4,2)||成绩[:decimal(4,2,1)]
phone||bigint||电话号码[:phone_number]
email||varchar(64)||家庭网络邮箱[:email]
ip||varchar(32)||IP地址[:ipv4]
address||text||家庭地址[:address]

meta.txt文件中每行数据为元数据的一个字段描述,以||分割为三列,若以#开头,则忽略该行。

第一列:字段名
第二列:表字段类型
第三列:字段注释,其中包含构造规则标识
name不加标记则会随机产生20字符内的字符串,可以加上改为:学生名字[:name]

其中学校名字[:enum(file://names.txt)]表示从本地文件names.txt中读取枚举数据,表示学校名称只能从下面这5所学校中随机产生。names.txt内容如下:

names.txt内容:

清华中学
人和中心
广东中学
猪场
旧大院

后面将详细介绍构造规则说明

注意:meta.txt和names.txt需要放在同一个目录下,再运行datafaker命令 如果没有enum类型从文件读取数据,则不需要names.txt文件

datafaker mysql mysql+mysqldb://root:123456@localhost:3306/test_datafaker stu 10 --outprint --meta meta.txt --outspliter ,,

在windows Powershell里面运行会出错,在普通的cmd 里面运行
在这里插入图片描述

写入:

datafaker rdb mysql+mysqldb://root:123456@localhost:3306/test_datafaker?charset=utf8 stu 10 --meta meta.txt 

则将直接写入mysql中

若要再次运行,需要修改meta.txt文件中为id[:inc(id,11)]起始值为11或更大值,不然数据库会报主键重复错误。

修改meta.txt

meta.txt

id||int||自增id[:inc(id,1)]
name||varchar(20)||学生名字[:name]
school||varchar(20)||学校名字[:enum(file://names.txt)]
nickname||varchar(20)||学生小名[:enum(鬼泣, 高小王子, 歌神, 逗比)]
age||int||学生年龄[:age]
class_num||int||班级人数[:int(10, 100)]
score||decimal(4,2)||成绩[:decimal(4,2,1)]
phone||bigint||电话号码[:phone_number]
email||varchar(64)||家庭网络邮箱[:email]
ip||varchar(32)||IP地址[:ipv4]
address||text||家庭地址[:address]

111

datafaker mysql mysql+mysqldb://root:123456@localhost:3306/test_datafaker stu 10 --outprint --meta meta.txt

在这里插入图片描述
这里面的名字和地址都是 自动生成的,还是比较牛的

风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。