如何优雅地用python生成模拟数据

python faker的使用

Faker是一个Python包,开源的GITHUB项目,主要用来创建伪数据,使用Faker包,无需再手动生成或者手写随机数来生成数据,只需要调用Faker提供的方法,即可完成数据的生成。本文参考文章 faker官方文档

上次在讲解python格式化地址的时候提到了要出一期视频,关于Faker的使用教程的,它来了

安装

pip install Faker

使用

from faker import Faker
faker=Faker(locale='zh_CN')
from faker import Faker
faker=Faker(locale='zh_CN')

地理信息类

# city_suffix():市,县
print('city_suffix', faker.city_suffix())
# country():国家
print('country', faker.country())
# country_code():国家编码
print('country_code', faker.country_code())
# district():区
print('district', faker.district())
# latitude():地理坐标(纬度)
print('latitude', faker.latitude())
# longitude():地理坐标(经度)
print('longitude', faker.longitude())
# postcode():邮编
print('postcode', faker.postcode())
# province():省份 (zh_TW没有此方法)
print('province', faker.province())
# address():详细地址
print('address', faker.address())
# street_address():街道地址
print('street_address', faker.street_address())
# street_name():街道名
print('street_name', faker.street_name())
# street_suffix():街、路
print('street_suffix', faker.street_suffix())
city_suffix 县
country 瑞士
country_code EG
district 白云
latitude -3.802822
longitude -104.749853
postcode 515776
province 西藏自治区
address 澳门特别行政区沈阳县南湖兴安盟街B座 298204
street_address 斯路a座
street_name 刘街
street_suffix 街

数字类

# numerify():三位随机数字
print('numerify', faker.numerify())
# random_digit():0~9随机数
print('random_digit', faker.random_digit())
# random_digit_not_null():1~9的随机数
print('random_digit_not_null', faker.random_digit_not_null())
# random_int():随机数字,默认0~9999,可以通过设置min,max来设置
print('random_int', faker.random_int())
# random_number():随机数字,参数digits设置生成的数字位数
print('random_number', faker.random_number())
# pyfloat():left_digits=5 #生成的整数位数, right_digits=2 #生成的小数位数, positive=True #是否只有正数
print('pyfloat', faker.pyfloat(left_digits=5, right_digits=10, positive=False))
# pyint():随机Int数字
print('pyint', faker.pyint())
# pydecimal():随机Decimal数字
print('pydecimal', faker.pydecimal())
numerify 617
random_digit 7
random_digit_not_null 4
random_int 6946
random_number 77948
pyfloat 29172.8052223595
pyint 947
pydecimal -9705319207208612501545.811314881298419529765666280148623635186

随机集合

# profile():随机生成档案信息
print('profile', faker.profile())

# simple_profile():随机生成简单档案信息
print('simple_profile', faker.simple_profile())
# pyiterable()
print('pyiterable', faker.pyiterable())
# pylist()
print('pylist', faker.pylist())
# pyset()
print('pyset', faker.pyset())
# pystruct()
print('pystruct', faker.pystruct())
# pytuple()
print('pytuple', faker.pytuple())
# pydict()
print('pydict', faker.pydict())
profile {'job': '办事处/分公司/分支机构经理', 'company': '诺依曼软件信息有限公司', 'ssn': '420302196212191078', 'residence': '甘肃省成都县普陀宁德路w座 531732', 'current_location': (Decimal('-64.2196785'), Decimal('-164.567579')), 'blood_group': 'A+', 'website': ['http://www.63.cn/', 'https://www.deng.cn/', 'http://www.dong.cn/'], 'username': 'nazhou', 'name': '辛柳', 'sex': 'M', 'address': '山西省玉梅县永川张家港路Z座 235904', 'mail': '[email protected]', 'birthdate': datetime.date(1967, 9, 19)}
simple_profile {'username': 'qiangbai', 'name': '贾颖', 'sex': 'F', 'address': '澳门特别行政区玉华市大东深圳街Q座 358582', 'mail': '[email protected]', 'birthdate': datetime.date(1979, 6, 29)}
pyiterable {'https://www.maozeng.cn/app/categories/author/', 'https://www.47.cn/terms/', Decimal('3373604707412099989898153787380589607825384863128704863389997768146744375237.20555771487158522955999621364872105010427822056'), 'ZklwCXYDnTTdkHoXdCxz', 'WgWjZRdaYFIcmhisZTGw', 9814.71706749687, -72.5311411499134, '[email protected]', 'WnUnOkvQgiBWTiYivDZm', 'bygozqEqBJMjQQjfyyHm', -6800825986307.4, 4543}
pylist ['HhuHlnsZUNVMwhGzJlFA', 'http://www.guiyingzhou.cn/', 5024, 'WIeOdutfPvfqDTojRITV', 'zSueaGRggrRyLcIUNhDH', datetime.datetime(1985, 4, 2, 22, 58, 6), 'nOabKGwOVZRwIZWPqNBi', 'LBwvjnfbYZZYHXLLvcmG', 'OEIYnuzQxLdgyOOxTxZK', 8901, 1811, 'KDjVpoatMDPCFkziWLTy']
pyset {'oJARksoCSTSxnIwbuXKN', Decimal('-83571026066997316223531648468872535284355141105671466437989439161.8120928778166569014500349959991580217968444664'), 50.86722412041, 1467226103.1629, -7.93224347096076, 'http://www.long.cn/privacy.jsp', Decimal('956743502459637986258265434701491050141602711070218002431088.406055588301631590704873788841974753913231402736')}
pystruct (['https://www.leili.cn/register.asp', 'https://67.cn/faq.htm', 'RjVRiTzDRQARXgAFihzI', 'rfVBwUycffjfpTznLsnd', datetime.datetime(1980, 11, 28, 5, 7, 23), '[email protected]', 2971, 'iCtQmgtzmuAbnBNksIQs', 6115, 'xIQgNiLsBlMFBCcofBnS'], {'合作': Decimal('-46.702294634473665210705878258307357926'), '类别': Decimal('13153845283484198297787371954731549936599646162911154034474.14356671708603687343627243460651398006215561502917511323820'), '影响': 'RnLiGrrKKJrvWDxcfnMb', '看到': 'EDotwjrkeSsKgLrCkvNP', '关系': 'eNPRsEPXhGCNkhyWoqsh', '质量': 'http://www.weihan.cn/', '帖子': 'wVzadAcbrrohgQtrAzpf', '一次': Decimal('10845837324860182768635292686667168902637254929877903291718454916492498960017.604745499'), '过程': 28474086435945.3, '可是': datetime.datetime(1998, 11, 13, 7, 15, 9)}, {'准备': {0: 3675, 1: ['[email protected]', 'OJoJCANQvZvkAdYxgKwn', datetime.datetime(1974, 4, 28, 5, 21, 54)], 2: {0: -7.36462675287523, 1: 4925502293.3432, 2: ['https://lv.cn/wp-content/author/', Decimal('-7978738727504740089112759095951237.0439166217563516599788352943369126015179771895974362068274728613222')]}}, '等级': {1: -5954.31482255049, 2: ['sGMSyWErnwrjqEwcZAef', Decimal('-155440068874200725091557253786287001605741466478.46'), 740657.495033703], 3: {1: 'ilLtacXwEtZEyObpzGmV', 2: '[email protected]', 3: ['YQODQcpaOPEYJjMkobMi', Decimal('3358734086246498111407695260381508754611780241372718236471.8005500136759971106738428998132697563321913956016317602586621633082267684107156744780267225479')]}}, '工作': {2: 'njrtOUOhgyfgkIdSlRtC', 3: [187.506865651419, 'lRlYwmgMowMRByCxJGuE', '[email protected]'], 4: {2: 6864057172.88083, 3: -70.3461156709465, 4: ['cCXqZekUusFCNStdUczz', 7230]}}, '解决': {3: datetime.datetime(2003, 3, 31, 3, 4, 33), 4: [2831, '[email protected]', '[email protected]'], 5: {3: 'NsEDOyqAQxziAFTPVGCW', 4: 'kAeyFOOZyxgeyLLAyFwd', 5: [6609, 'VQZjzMomdcitUSYpmrue']}}, '进行': {4: 'meOjbWYIwORHAyhABRbJ', 5: [Decimal('48635485080696686797239341623487737997775506023708802806688066429339085821111798482447684249196.7481668175642609248848125822416460'), datetime.datetime(1992, 10, 31, 2, 2, 17), '[email protected]'], 6: {4: 'FxlCuyfYrLjWoiyWwzZT', 5: '[email protected]', 6: ['aLhnzwaIHKqsvyCdbRse', '[email protected]']}}, '报告': {5: datetime.datetime(2000, 4, 19, 21, 7, 11), 6: ['XrqMMongxXQXogjcjzXN', 'YoGRijQZQxqorgxOUJIa', 'ZMTUqGgJNfaMBcVHlFXm'], 7: {5: 920, 6: 'VDISjmiyLoeXTqOFGcct', 7: ['http://www.min.net/wp-content/explore/index.html', 'http://kt.cn/homepage/']}}, '那些': {6: 'iRGDcfDLIxlsozzFIUpd', 7: ['BvJxbdSaAEHyIMcHZgiq', 5579, 'https://www.kv.cn/explore/search.php'], 8: {6: 770, 7: datetime.datetime(2017, 11, 17, 23, 51, 53), 8: ['QgwEEQPNPaulSSfVslXG', 'hOvpgiZCXunsGofrajyI']}}, '环境': {7: '[email protected]', 8: [2836, 'AjfrzACdCTVtaNuQszOH', Decimal('89071168708384194122892821772270902450012469497119862835336311.61522017674615609878444385261937405175908255942127218747219625')], 9: {7: 1894, 8: 'PfyycooEoZzIKnjlSmUK', 9: [datetime.datetime(1973, 7, 2, 13, 53, 29), '[email protected]']}}, '有关': {8: datetime.datetime(1991, 10, 7, 19, 7, 45), 9: ['QIHZEibyKfjxefjOpAQg', 'PmAJyfBTLwGwrVKOyTpB', 'MPIjDzFYrNXUekvvjiGC'], 10: {8: 345, 9: Decimal('8327260986335.6812325959251321991755864367211588357553417817446907006457917999788853477347404593592973323038284'), 10: [3308, 'http://www.tl.cn/categories/wp-content/categories/login/']}}, '已经': {9: 'hZZVToKsndyXNyGbRERX', 10: [9720692823966.43, Decimal('-8735655405156743007447967909339.02058883'), 444], 11: {9: 'iCMhujubUtcvZFNvWzdf', 10: 'https://changtian.cn/blog/privacy/', 11: [datetime.datetime(2005, 8, 3, 7, 15, 26), 'YaHNvGfdXsRQSMvykJBN']}}})
pytuple ('HaHZOzJVSGiJJccMjCEE', 562.537262560239, 'xZZtQsadFCfVpEhwpwFE', '[email protected]', '[email protected]', 'https://yangmo.cn/main/categories/category/login/', 'AIgGAPdBybYXkfGIvyGX', 'ujUKqNEDulRmpkPoKApo')
pydict {'电影': 'fPRHiHOUdwkotvNCCdKZ', '以及': 'vzivcAgLvauzgXkzvsIq', '程序': 6748, '次数': 'tPSyiXtMCkqreGnjINce', '一下': 'ArRnrdZsBZBoQWixPvEL', '设计': 'NnWWJCxXMxdOEmbScxaD', '积分': 'http://37.cn/', '只是': datetime.datetime(2001, 6, 10, 23, 55, 2)}

工具库类别

# currency_code():货币编码
print('currency_code', faker.currency_code())

# color_name():随机颜色名
print('color_name', faker.color_name())
# hex_color():随机HEX颜色
print('hex_color', faker.hex_color())
# rgb_color():随机RGB颜色
print('rgb_color', faker.rgb_color())
# safe_color_name():随机安全色名
print('safe_color_name', faker.safe_color_name())
# safe_hex_color():随机安全HEX颜色
print('safe_hex_color', faker.safe_hex_color())
# isbn10():随机ISBN(10位)
print('isbn10', faker.isbn10())
# isbn13():随机ISBN(13位)
print('isbn13', faker.isbn13())
# lexify():替换所有问号(“?”)带有随机字母的事件。
print('lexify', faker.lexify())
currency_code LAK
color_name Gainsboro
hex_color #6f16f3
rgb_color 238,163,89
safe_color_name purple
safe_hex_color #441111
isbn10 0-385-45429-5
isbn13 978-0-344-49870-1
lexify hHgd

时间信息类

# am_pm():AM/PM
print('am_pm', faker.am_pm())
# century():随机世纪
print('century', faker.century())
# date():随机日期
print('date', faker.date())
# date_between():随机生成指定范围内日期,参数:start_date,end_date取值:具体日期或者today,-30d,-30y类似
print('date_between', faker.date_between())
# date_between_dates():随机生成指定范围内日期,用法同上
print('date_between_dates', faker.date_between_dates())
# date_object():随机生产从1970-1-1到指定日期的随机日期。
print('date_object', faker.date_object())
# date_this_month():
print('date_this_month',faker.date_this_month())
# date_this_year():
print('date_this_year', faker.date_this_year())
# date_time():随机生成指定时间(1970年1月1日至今)
print('date_time',faker.date_time())
# date_time_ad():生成公元1年到现在的随机时间
print('date_time_ad',faker.date_time_ad())
# date_time_between():用法同dates
print('date_time_between',faker.date_time_between())
# future_date():未来日期
print('future_date',faker.future_date())
# future_datetime():未来时间
print('future_datetime',faker.future_datetime())
# month():随机月份
print('month',faker.month())
# month_name():随机月份(英文)
print('month_name',faker.month_name())
# past_date():随机生成已经过去的日期
print('past_date',faker.past_date())
# past_datetime():随机生成已经过去的时间
print('past_datetime', faker.past_datetime())
# time():随机24小时时间
print('time', faker.time())
# time_object():随机24小时时间,time对象
print('time_object', faker.time_object())
# time_series():随机TimeSeries对象
print('time_series', faker.time_series())
# timezone():随机时区
print('timezone', faker.timezone())
# unix_time():随机Unix时间
print('unix_time', faker.unix_time())
# year():随机年份
print('year', faker.year())
am_pm AM
century XVIII
date 2004-09-18
date_between 2018-04-11
date_between_dates 2023-08-06
date_object 1996-04-21
date_this_month 2023-08-03
date_this_year 2023-03-22
date_time 1998-02-25 20:33:09
date_time_ad 1581-03-23 06:20:22
date_time_between 2014-09-22 19:13:21
future_date 2023-08-31
future_datetime 2023-08-07 03:53:03
month 05
month_name 五月
past_date 2023-07-30
past_datetime 2023-07-13 02:17:22
time 02:06:15
time_object 20:34:37
time_series <generator object Provider.time_series at 0x115911d60>
timezone Europe/Skopje
unix_time 1113767873
year 2004

文本、加密类

因为文章的篇幅过长,其他的API仅作罗列,不做验证和展示,感兴趣的伙伴可以自行查找验证

# pystr():随机字符串

# random_element():随机字母

# random_letter():随机字母

# paragraph():随机生成一个段落

# paragraphs():随机生成多个段落,通过参数nb来控制段落数,返回数组

# sentence():随机生成一句话

# sentences():随机生成多句话,与段落类似

# text():随机生成一篇文章(不要幻想着人工智能了,至今没完全看懂一句话是什么意思)

# word():随机生成词语

# words():随机生成多个词语,用法与段落,句子,类似

# binary():随机生成二进制编码

# boolean():True/False

# language_code():随机生成两位语言编码

# locale():随机生成语言/国际 信息

# md5():随机生成MD5

# null_boolean():NULL/True/False

# password():随机生成密码,可选参数:length:密码长度;special_chars:是否能使用特殊字符;digits:是否包含数字;upper_case:是否包含大写字母;lower_case:是否包含小写字母

# sha1():随机SHA1

# sha256():随机SHA256

# uuid4():随机UUID

文件信息类

# file_extension():随机文件扩展名
print('file_extension', faker.file_extension())
# file_name():随机文件名(包含扩展名,不包含路径)
print('file_name', faker.file_name())
# file_path():随机文件路径(包含文件名,扩展名)
print('file_path', faker.file_path())
# mime_type():随机mime Type
print('mime_type', faker.mime_type())
file_extension js
file_name 电子.mp3
file_path /只是/还有.wav
mime_type multipart/alternative

浏览器信息

强推,用于爬虫的时候真香

# chrome():随机生成Chrome的浏览器user_agent信息
print('chrome', faker.chrome())
# firefox():随机生成FireFox的浏览器user_agent信息
print('firefox', faker.firefox())
# internet_explorer():随机生成IE的浏览器user_agent信息
print('internet_explorer', faker.internet_explorer())
# opera():随机生成Opera的浏览器user_agent信息
print('opera', faker.opera())
# safari():随机生成Safari的浏览器user_agent信息
print('safari', faker.safari())
# linux_platform_token():随机Linux信息
print('linux_platform_token', faker.linux_platform_token())
# user_agent():随机user_agent信息
print('user_agent', faker.user_agent())
chrome Mozilla/5.0 (Windows 95) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/55.0.807.0 Safari/535.1
firefox Mozilla/5.0 (Macintosh; Intel Mac OS X 10_5_6; rv:1.9.2.20) Gecko/9814-12-10 00:45:58 Firefox/3.6.16
internet_explorer Mozilla/5.0 (compatible; MSIE 9.0; Windows 95; Trident/3.1)
opera Opera/8.70.(Windows NT 5.0; ca-ES) Presto/2.9.182 Version/10.00
safari Mozilla/5.0 (Windows; U; Windows NT 6.2) AppleWebKit/533.18.3 (KHTML, like Gecko) Version/4.1 Safari/533.18.3
linux_platform_token X11; Linux i686
user_agent Mozilla/5.0 (Android 2.3.5; Mobile; rv:18.0) Gecko/18.0 Firefox/18.0

网路信息类

# domain_name():生成域名

# domain_word():域词(即,不包含后缀)

# ipv4():随机IP4地址

# ipv6():随机IP6地址

# mac_address():随机MAC地址

# tld():网址域名后缀(.com,.net.cn,等等,不包括.)

# uri():随机URI地址

# uri_extension():网址文件后缀

# uri_page():网址文件(不包含后缀)

# uri_path():网址文件路径(不包含文件名)

# url():随机URL地址

# user_name():随机用户名

# image_url():随机URL地址

个人账户类

# ascii_company_email():随机ASCII公司邮箱名

# ascii_email():随机ASCII邮箱

# ascii_free_email():

# ascii_safe_email():

# company_email():

# email():

# free_email():

# free_email_domain():

# safe_email():安全邮箱

基础信息类

其实在这个api中几乎全部包含了

# profile():随机生成档案信息
print('profile', faker.profile())
# ssn():生成身份证号

# bs():随机公司服务名

# company():随机公司名(长)

# company_prefix():随机公司名(短)

# company_suffix():公司性质

# credit_card_expire():随机信用卡到期日

# credit_card_full():生成完整信用卡信息

# credit_card_number():信用卡号

# credit_card_provider():信用卡类型

# credit_card_security_code():信用卡安全码

# job():随机职位

# 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():随机生成手机号段

猜你喜欢

转载自blog.csdn.net/weixin_55768452/article/details/132132772