【python模块】 伪数据生产者:faker模块

faker模块主要是用来创建伪数据,无需手动生成或者手写随机数来生成数据,可以利用faker快速完成伪造大量测试数据的工作。

一、虚拟环境内用百度源安装

(py_100) PS E:\python_100> pip install Faker -i https://mirror.baidu.com/pypi/simple
Looking in indexes: https://mirror.baidu.com/pypi/simple
Collecting Faker
  Downloading https://mirror.baidu.com/pypi/packages/56/a8/8fdebfede1520d4e9b8d8969799e3b2e4efd77b4f9ab4fee3799f63de5a9/Faker-9.5.2-py3-none-any.whl (1.2 MB)
     |████████████████████████████████| 1.2 MB 6.8 MB/s
Collecting text-unidecode==1.3
  Downloading https://mirror.baidu.com/pypi/packages/a6/a5/c0b6468d3824fe3fde30dbb5e1f687b291608f9473681bbf7dabbf5a87d7/text_unidecode-1.3-py2.py3-none-any.whl (78 kB)
     |████████████████████████████████| 78 kB 1.3 MB/s
Requirement already satisfied: python-dateutil>=2.4 in e:\python_100\py_100\lib\site-packages (from Faker) (2.8.2)
Requirement already satisfied: six>=1.5 in e:\python_100\py_100\lib\site-packages (from python-dateutil>=2.4->Faker) (1.16.0)
Installing collected packages: text-unidecode, Faker
Successfully installed Faker-9.5.2 text-unidecode-1.3

二、生成一个伪数据

from faker import Faker

faker = Faker('zh_CN')  # 默认英文,zh_CN为中文

print(faker.country())     # 国家
print(faker.city())        # 城市
print(faker.street_name())        # 城市
print(faker.random_digit())  # 0-9随机数
print(faker.date())    # 日期
print(faker.year())    # 年份
print(faker.month())   # 月份
print(faker.email())   # 邮箱
print(faker.job())     # 职位
print(faker.word())    # 单词
print(faker.ssn())     # 身份证号
print(faker.phone_number())  # 手机号

输出结果:
乌兹别克斯坦
荣市
张路
9
1998-07-08
2016
01
pingliu@example.org
技术总监/经理
免费
450521199304094987
18564169159

三、使用列表推导式批量生成

from faker import Faker

faker = Faker('zh_CN')  # 默认英文,zh_CN为中文

print([faker.country() for i in range(10)])     # 国家
print([faker.city() for i in range(10)])        # 城市
print([faker.street_name() for i in range(10)])        # 城市
print([faker.random_digit() for i in range(10)])  # 0-9随机数
print([faker.date() for i in range(10)])    # 日期
print([faker.year() for i in range(10)])    # 年份
print([faker.month() for i in range(10)])   # 月份
print([faker.email() for i in range(10)])   # 邮箱
print([faker.job() for i in range(10)])     # 职位
print([faker.word() for i in range(10)])    # 单词
print([faker.ssn() for i in range(10)])     # 身份证号
print([faker.phone_number() for i in range(10)])  # 手机号

输出结果:
['蒙特塞拉特岛', '诺福克岛', '葡萄牙', '南非', '加那利群岛', '安圭拉岛英', '阿塞拜疆', '阿尔巴尼亚', '阿拉伯联合酋长国', '丹麦']
['桂英县', '沈阳市', '文市', '上海县', '台北市', '俊县', '拉萨市', '平市', '福州县', '石家庄市']
['惠州路', '哈尔滨街', '余路', '马鞍山路', '顾街', '胡路', '江街', '呼和浩特街', '沈阳街', '孙街']
[0, 0, 6, 6, 1, 2, 9, 8, 8, 6]
['1978-04-18', '1972-02-25', '2003-12-14', '2007-08-11', '1993-09-10', '1981-10-18', '2006-01-17', '1989-03-22', '1982-12-10', '1992-05-31']
['1992', '2013', '1996', '2016', '1981', '2007', '2001', '2004', '2010', '1998']
['01', '01', '10', '07', '09', '02', '09', '05', '07', '12']
['wei11@example.com', 'fang96@example.org', 'yanxiuying@example.com', 'xxiao@example.net', 'yxiao@example.net', 'yanmin@example.org', 'mwei@example.org', 'jiexiong@example.net', 'songxiulan@example.org', 'yangqiao@example.com']
['后勤', '客服总监', '用户体验(UE/UX)设计师', 'IT-品管、技术支持及其它', '食品/饮料研发', '培训督导', '配菜/打荷', 'IT-管理', '知识产权/专利/商标', '其他']
['要求', '还有', '全国', '法律', '原因', '你的', '部分', '网上', '看到', '这种']
['610602195310069575', '430202196903101277', '530901194507207543', '510108194304264967', '441500195801239605', '530800193703310787', '410403194204089234', '451227196308179303', '654323195106290084', '500109200307296078']
['18210174866', '15800311418', '18503088388', '18714565791', '18840823240', '15869948595', '14543070715', '18703041030', '18826615267', '18771006360']

四、常用函数介绍

4.1 常用数据

函数生成伪数据
city_suffix()市,县
country()国家
country_code()国家编码
district()
geo_coordinate()地理坐标
latitude()地理坐标(纬度)
longitude()地理坐标(经度)
lexify()替换所有问号(“?”)带有随机字母的事件。
numerify()三位随机数字
postcode()邮编
province()省份
street_address()街道地址
street_name()街道名
street_suffix()街、路
random_digit()0~9随机数
random_digit_not_null()1~9的随机数
random_element()随机字母
random_int()随机数字,默认0~9999,可以通过设置min,max来设置
random_letter()随机字母
random_number()随机数字,参数digits设置生成的数字位数
color_name()随机颜色名
hex_color()随机HEX颜色
rgb_color()随机RGB颜色
safe_color_name()随机安全色名
safe_hex_color()随机安全HEX颜色
bs()随机公司服务名
company()随机公司名(长)
company_prefix()随机公司名(短)
company_suffix()公司性质
credit_card_expire()随机信用卡到期日
credit_card_full()生成完整信用卡信息
credit_card_number()信用卡号
credit_card_provider()信用卡类型
credit_card_security_code()信用卡安全码
currency_code()货币编码
am_pm()AM/PM
century()随机世纪
date()随机日期
date_between()随机生成指定范围内日期,参数
date_between_dates()随机生成指定范围内日期,用法同上
date_object()随机生产从1970-1-1到指定日期的随机日期。
date_this_month()
date_this_year()
date_time()随机生成指定时间(1970年1月1日至今)
date_time_ad()生成公元1年到现在的随机时间
date_time_between()用法同dates
future_date()未来日期
future_datetime()未来时间
month()随机月份
month_name()随机月份(英文)
past_date()随机生成已经过去的日期
past_datetime()随机生成已经过去的时间
time()随机24小时时间
timedelta()随机获取时间差
time_object()随机24小时时间,time对象
time_series()随机TimeSeries对象
timezone()随机时区
unix_time()随机Unix时间
year()随机年份
file_extension()随机文件扩展名
file_name()随机文件名(包含扩展名,不包含路径)
file_path()随机文件路径(包含文件名,扩展名)
mime_type()随机mime Type
ascii_company_email()随机ASCII公司邮箱名
ascii_email()随机ASCII邮箱
ascii_free_email()
ascii_safe_email()
company_email()
domain_name()生成域名
domain_word()域词(即,不包含后缀)
email()
free_email()
free_email_domain()
f.safe_email()安全邮箱
f.image_url()随机URL地址
ipv4()随机IP4地址
ipv6()随机IP6地址
mac_address()随机MAC地址
tld()网址域名后缀(.com,.net.cn,等等,不包括.)
uri()随机URI地址
uri_extension()网址文件后缀
uri_page()网址文件(不包含后缀)
uri_path()网址文件路径(不包含文件名)
url()随机URL地址
user_name()随机用户名
isbn10()随机ISBN(10位)
isbn13()随机ISBN(13位)
job()随机职位
paragraph()随机生成一个段落
paragraphs()随机生成多个段落,通过参数nb来控制段落数,返回数组
sentence()随机生成一句话
sentences()随机生成多句话,与段落类似
text()随机生成一篇文章(不要幻想着人工智能了,至今没完全看懂一句话是什么意思)
word()随机生成词语
words()随机生成多个词语,用法与段落,句子,类似
binary()随机生成二进制编码
boolean()True/False
language_code()随机生成两位语言编码
locale()随机生成语言/国际 信息
md5()随机生成MD5
null_boolean()NULL/True/False
password()随机生成密码,可选参数
sha1()随机SHA1
sha256()随机SHA256
uuid4()随机UUID
first_name()
first_name_female()女性名
first_name_male()男性名
first_romanized_name()罗马名
last_name()
last_name_female()女姓
last_name_male()男姓
last_romanized_name()
name()随机生成全名
name_female()男性全名
name_male()女性全名
romanized_name()罗马名
msisdn()移动台国际用户识别码,即移动用户的ISDN号码
phone_number()随机生成手机号
phonenumber_prefix()随机生成手机号段
profile()随机生成档案信息
simple_profile()随机生成简单档案信息

4.2 随机生成指定类型数据

函数生成伪数据
pybool()布尔类型
pydecimal()数字
pydict()字典
pyfloat()left_digits=5 #生成的整数位数, right_digits=2 #生成的小数位数, positive=True #是否只有正数
pyint()整型
pyiterable()迭代器
pylist()列表
pyset()集合
pystr()字符串
pystruct()结构体
pytuple()元组

4.3 随机生成常用数据

函数生成伪数据
ssn()生成身份证号
chrome()随机生成Chrome的浏览器user_agent信息
firefox()随机生成FireFox的浏览器user_agent信息
internet_explorer()随机生成IE的浏览器user_agent信息
opera()随机生成Opera的浏览器user_agent信息
safari()随机生成Safari的浏览器user_agent信息
linux_platform_token()随机Linux信息
user_agent()随机user_agent信息
  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

岳涛@心馨电脑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值