82 lines
2.3 KiB
Python
82 lines
2.3 KiB
Python
#encoding=utf-8
|
||
import csv
|
||
from faker import Faker
|
||
from datetime import datetime
|
||
|
||
"""
|
||
生成尽可能真实的假数据
|
||
|
||
使用方法:
|
||
1.安装依赖包
|
||
pip install faker
|
||
2.执行脚本
|
||
python faker_data.py
|
||
3.当前目录,查看生成的数据
|
||
"""
|
||
|
||
#保存为csv文件
|
||
def save_data_csv(file_name,lines=100):
|
||
#获取数据
|
||
datas = faker_data(lines)
|
||
#保存
|
||
with open(file_name,'w+',encoding='utf-8',newline='') as file_csv:
|
||
writer = csv.writer(file_csv, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)
|
||
writer.writerows(datas)
|
||
|
||
#生成数据,默认10条
|
||
def faker_data(lines=10):
|
||
#指定数据的国家地区
|
||
f = Faker('zh-CN')
|
||
#定义一个列表,用来存放所有数据
|
||
datas = []
|
||
#标题
|
||
title = ["uuid","id","name","mobile","ssn","sex","email","job","address","actime_time"]
|
||
#title2 = ["唯一标识","编号","姓名","手机号","身份证号","性别","邮箱","职业","家庭地址","获取时间"]
|
||
#添加标题到列表中
|
||
datas.append(title)
|
||
#datas.append(title2)
|
||
#开始按照标题的顺序,生成N条数据
|
||
for i in range(0,lines):
|
||
#定义一个列表,用来存一行数据
|
||
data = []
|
||
#uuid
|
||
data.append(f.uuid4())
|
||
#编号,001,不足3位的左边用0来补齐
|
||
data.append(str(i+1).rjust(3,'0'))
|
||
#姓名
|
||
data.append(f.name())
|
||
#手机号
|
||
data.append(f.phone_number())
|
||
#身份证
|
||
ssn = f.ssn()
|
||
data.append(ssn)
|
||
#性别,根据身份证的第17位来判断
|
||
ssn_sex = int(ssn[16:17])
|
||
#01:男,02:女
|
||
if ssn_sex % 2:
|
||
sex = "01"
|
||
else:
|
||
sex = "02"
|
||
data.append(sex)
|
||
#邮箱
|
||
data.append(f.email())
|
||
#职业
|
||
data.append(f.job())
|
||
#地址,让其更加复合中国的地址
|
||
address = f.address()[:-9] + str(f.pyint(min_value=0, max_value=999))+ "号"
|
||
data.append(address)
|
||
#获取当前时间
|
||
actime_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
|
||
data.append(actime_time)
|
||
#将这一行数据添加到datas中
|
||
datas.append(data)
|
||
#返回所有的数据
|
||
return datas
|
||
|
||
|
||
if __name__ == '__main__':
|
||
#文件名
|
||
file_name = 'test.csv'
|
||
save_data_csv(file_name)
|
||
|