使用Python實現(xiàn)優(yōu)雅生成假數(shù)據(jù)
Faker是一個Python包,開源的GITHUB項目,主要用來創(chuàng)建偽數(shù)據(jù),使用Faker包,無需再手動生成或者手寫隨機數(shù)來生成數(shù)據(jù),只需要調(diào)用Faker提供的方法,即可完成數(shù)據(jù)的生成。本文參考文章 faker官方文檔
上次在講解python格式化地址的時候提到了要出一期視頻,關(guān)于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():區(qū) print('district', faker.district()) # latitude():地理坐標(緯度) print('latitude', faker.latitude()) # longitude():地理坐標(經(jīng)度) 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 西藏自治區(qū) address 澳門特別行政區(qū)沈陽縣南湖興安盟街B座 298204 street_address 斯路a座 street_name 劉街 street_suffix 街
數(shù)字類
# numerify():三位隨機數(shù)字 print('numerify', faker.numerify()) # random_digit():0~9隨機數(shù) print('random_digit', faker.random_digit()) # random_digit_not_null():1~9的隨機數(shù) print('random_digit_not_null', faker.random_digit_not_null()) # random_int():隨機數(shù)字,默認0~9999,可以通過設(shè)置min,max來設(shè)置 print('random_int', faker.random_int()) # random_number():隨機數(shù)字,參數(shù)digits設(shè)置生成的數(shù)字位數(shù) print('random_number', faker.random_number()) # pyfloat():left_digits=5 #生成的整數(shù)位數(shù), right_digits=2 #生成的小數(shù)位數(shù), positive=True #是否只有正數(shù) print('pyfloat', faker.pyfloat(left_digits=5, right_digits=10, positive=False)) # pyint():隨機Int數(shù)字 print('pyint', faker.pyint()) # pydecimal():隨機Decimal數(shù)字 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': '辦事處/分公司/分支機構(gòu)經(jīng)理', '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': 'smao@hotmail.com', 'birthdate': datetime.date(1967, 9, 19)} simple_profile {'username': 'qiangbai', 'name': '賈穎', 'sex': 'F', 'address': '澳門特別行政區(qū)玉華市大東深圳街Q座 358582', 'mail': 'yanglin@gmail.com', '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, 'wanjing@example.net', '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), 'mintao@example.org', 2971, 'iCtQmgtzmuAbnBNksIQs', 6115, 'xIQgNiLsBlMFBCcofBnS'], {'合作': Decimal('-46.702294634473665210705878258307357926'), '類別': Decimal('13153845283484198297787371954731549936599646162911154034474.14356671708603687343627243460651398006215561502917511323820'), '影響': 'RnLiGrrKKJrvWDxcfnMb', '看到': 'EDotwjrkeSsKgLrCkvNP', '關(guān)系': 'eNPRsEPXhGCNkhyWoqsh', '質(zhì)量': 'http://www.weihan.cn/', '帖子': 'wVzadAcbrrohgQtrAzpf', '一次': Decimal('10845837324860182768635292686667168902637254929877903291718454916492498960017.604745499'), '過程': 28474086435945.3, '可是': datetime.datetime(1998, 11, 13, 7, 15, 9)}, {'準備': {0: 3675, 1: ['ming01@example.net', '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: 'ming50@example.net', 3: ['YQODQcpaOPEYJjMkobMi', Decimal('3358734086246498111407695260381508754611780241372718236471.8005500136759971106738428998132697563321913956016317602586621633082267684107156744780267225479')]}}, '工作': {2: 'njrtOUOhgyfgkIdSlRtC', 3: [187.506865651419, 'lRlYwmgMowMRByCxJGuE', 'luoyan@example.net'], 4: {2: 6864057172.88083, 3: -70.3461156709465, 4: ['cCXqZekUusFCNStdUczz', 7230]}}, '解決': {3: datetime.datetime(2003, 3, 31, 3, 4, 33), 4: [2831, 'gangzeng@example.com', 'ming89@example.org'], 5: {3: 'NsEDOyqAQxziAFTPVGCW', 4: 'kAeyFOOZyxgeyLLAyFwd', 5: [6609, 'VQZjzMomdcitUSYpmrue']}}, '進行': {4: 'meOjbWYIwORHAyhABRbJ', 5: [Decimal('48635485080696686797239341623487737997775506023708802806688066429339085821111798482447684249196.7481668175642609248848125822416460'), datetime.datetime(1992, 10, 31, 2, 2, 17), 'blu@example.net'], 6: {4: 'FxlCuyfYrLjWoiyWwzZT', 5: 'kqiao@example.com', 6: ['aLhnzwaIHKqsvyCdbRse', 'guiying41@example.org']}}, '報告': {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']}}, '環(huán)境': {7: 'zengming@example.com', 8: [2836, 'AjfrzACdCTVtaNuQszOH', Decimal('89071168708384194122892821772270902450012469497119862835336311.61522017674615609878444385261937405175908255942127218747219625')], 9: {7: 1894, 8: 'PfyycooEoZzIKnjlSmUK', 9: [datetime.datetime(1973, 7, 2, 13, 53, 29), 'jieqian@example.net']}}, '有關(guān)': {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/']}}, '已經(jīng)': {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', 'yumin@example.net', 'axia@example.net', 'https://yangmo.cn/main/categories/category/login/', 'AIgGAPdBybYXkfGIvyGX', 'ujUKqNEDulRmpkPoKApo') pydict {'電影': 'fPRHiHOUdwkotvNCCdKZ', '以及': 'vzivcAgLvauzgXkzvsIq', '程序': 6748, '次數(shù)': 'tPSyiXtMCkqreGnjINce', '一下': 'ArRnrdZsBZBoQWixPvEL', '設(shè)計': '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():隨機生成指定范圍內(nèi)日期,參數(shù):start_date,end_date取值:具體日期或者today,-30d,-30y類似 print('date_between', faker.date_between()) # date_between_dates():隨機生成指定范圍內(nèi)日期,用法同上 print('date_between_dates', faker.date_between_dates()) # date_object():隨機生產(chǎn)從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年到現(xiàn)在的隨機時間 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():隨機生成已經(jīng)過去的日期 print('past_date',faker.past_date()) # past_datetime():隨機生成已經(jīng)過去的時間 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():隨機時區(qū) 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():隨機生成多個段落,通過參數(shù)nb來控制段落數(shù),返回數(shù)組 # sentence():隨機生成一句話 # sentences():隨機生成多句話,與段落類似 # text():隨機生成一篇文章(不要幻想著人工智能了,至今沒完全看懂一句話是什么意思) # word():隨機生成詞語 # words():隨機生成多個詞語,用法與段落,句子,類似 # binary():隨機生成二進制編碼 # boolean():True/False # language_code():隨機生成兩位語言編碼 # locale():隨機生成語言/國際 信息 # md5():隨機生成MD5 # null_boolean():NULL/True/False # password():隨機生成密碼,可選參數(shù):length:密碼長度;special_chars:是否能使用特殊字符;digits:是否包含數(shù)字;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
網(wǎng)路信息類
# domain_name():生成域名 # domain_word():域詞(即,不包含后綴) # ipv4():隨機IP4地址 # ipv6():隨機IP6地址 # mac_address():隨機MAC地址 # tld():網(wǎng)址域名后綴(.com,.net.cn,等等,不包括.) # uri():隨機URI地址 # uri_extension():網(wǎng)址文件后綴 # uri_page():網(wǎng)址文件(不包含后綴) # uri_path():網(wǎng)址文件路徑(不包含文件名) # 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():安全郵箱
基礎(chǔ)信息類
其實在這個api中幾乎全部包含了
# profile():隨機生成檔案信息 print('profile', faker.profile()) # ssn():生成身份證號 # bs():隨機公司服務(wù)名 # company():隨機公司名(長) # company_prefix():隨機公司名(短) # company_suffix():公司性質(zhì) # 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():隨機生成手機號段
到此這篇關(guān)于使用Python實現(xiàn)優(yōu)雅生成假數(shù)據(jù)的文章就介紹到這了,更多相關(guān)Python生成假數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一文搞懂Python中pandas透視表pivot_table功能
透視表是一種可以對數(shù)據(jù)動態(tài)排布并且分類匯總的表格格式?;蛟S大多數(shù)人都在Excel使用過數(shù)據(jù)透視表,也體會到它的強大功能,而在pandas中它被稱作pivot_table,今天通過本文給大家介紹Python中pandas透視表pivot_table功能,感興趣的朋友一起看看吧2021-11-11Python3中l(wèi)ambda表達式與函數(shù)式編程講解
今天小編就為大家分享一篇關(guān)于Python3中l(wèi)ambda表達式與函數(shù)式編程講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01如何在Python中利用matplotlib.pyplot畫出函數(shù)圖詳解
通過圖像可以直觀地學(xué)習(xí)函數(shù)變化、分布等規(guī)律,在學(xué)習(xí)函數(shù)、概率分布等方面效果顯著,下面這篇文章主要給大家介紹了關(guān)于如何在Python中利用matplotlib.pyplot畫出函數(shù)圖的相關(guān)資料,需要的朋友可以參考下2022-08-08Python3爬蟲教程之利用Python實現(xiàn)發(fā)送天氣預(yù)報郵件
這篇文章主要給大家介紹了關(guān)于Python3爬蟲教程之利用Python實現(xiàn)發(fā)送天氣預(yù)報郵件的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧2018-12-12