Faker生成的Mock数据写入Excel&CSV
依赖Faker包
pip install faker
方式一:写入Excel
文件
import xlwt
from faker import Faker
from xpinyin import Pinyin
def create_excel():
# 初始化生成数据的文化选项中文,默认en_US
faker = Faker("zh_CN")
# 引用第三方包,将汉字转为拼音且大写,并设置为人员的UMID编号
py = Pinyin()
# 标题信息
title = ['Name','UMID','Phone','SSN','Address','Email']
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('mockdata',cell_overwrite_ok=True) #添加sheet,参数重复写入
for i in range(50):
username = str(faker.name())
umid = py.get_pinyin(username, '', tone_marks=None, convert='upper')
mock = [username,umid,faker.phone_number(),f