简介
Faker库可用于随机生成测试用的虚假数据。
可生成的数据参考底部的参考链接。
安装:
python -m pip install faker
快速入门
from faker import Faker # 实例化一个对象,本地化使用中国 fk - Faker(locale="zh_CN") print(f"{fk.name()} 住在 {fk.address()}")
示例-生成数据并写入到MySQL
import pandas as pd from sqlalchemy import create_engine from faker import Faker import time fk = Faker(locale='zh-CN') def create_casefile(nums: int = 10, filename: str = "fakedata.csv"): """ 生成虚假数据文件 Parameters ---------- nums: int, default: 10 数据量 filename: str, default: "fakedata.csv" 虚假数据文件名 """ start_time = time.time() with open(filename, 'w', encoding='utf8', buffering=4096) as fobj: fobj.write("username,phone_number,address,company,job\n") # for i in range(nums): i = 1 while i <= nums: fobj.write(f"{fk.name()},{fk.phone_number()},{fk.address()},{fk.company()},{fk.job()}") if i != nums: fobj.write("\n") i += 1 end_time = time.time() print(f"共生成 {nums} 条数据, 耗时: {(end_time - start_time):.2f}") def load_to_mysql(filename: str = "fakedata.csv"): """ 将生成的虚拟数据写入到MySQL数据库 Parameters ---------- filename: str, default: "fakedata.csv" 虚假数据文件名 """ df = pd.read_csv(filename, sep=",") print(df.head()) eng = create_engine("mysql+pymysql://root:123456@192.168.0.10:3306/testdb") start_time = time.time() print("开始将数据写入到数据库") df.to_sql(name="fakedata", con=eng, if_exists="replace") end_time = time.time() print(f"数据库写入完成, 耗时: {(end_time - start_time):.2f}") if __name__ == '__main__': create_casefile(nums=100000) load_to_mysql(filename="fakedata.csv")