python faker 生成随机类型字符串

以前生成测试字符时,用random模块拼来拼去来生成随机串,如姓名,手机,身份证等,还是费一些功夫,不过有了faker模块,一切变得简单起来

基本使用:

from faker import Faker
fake = Faker()
print fake.name()   # Jeffrey Freeman
fake = Faker("zh_CN")
print fake.name()  # 潘杨
print dir(fake)  # 可以看到所有随机方法。


fake = Faker()
try:
    for i in dir(fake):
        if hasattr(fake, i):
            func = getattr(fake, i)
            if i.startswith('_') or i in ['add_provider', 'format', 'binary','get_formatter', 'parse' ,'provider','providers','pytuple','random']:
                pass
            else:
                print i,':',func()

        else:
            print('操作有误,请重新输入')
except Exception as e:
    pass


address : 962 Brown Mountain
East Mary, CT 04738
am_pm : PM
ascii_company_email : dawnhines@cantrell.com
ascii_email : richardsanna@yahoo.com
ascii_free_email : ejohnson@yahoo.com
ascii_safe_email : steven92@example.org
bank_country : GB
bban : XRKR3466353844591
boolean : False
bothify : 16 zD
bs : harness cross-media web-readiness
building_number : 2202
catch_phrase : Quality-focused solution-oriented hierarchy
century : XII
chrome : Mozilla/5.0 (Macintosh; Intel Mac OS X 10_5_0) AppleWebKit/5352 (KHTML, like Gecko) Chrome/40.0.802.0 Safari/5352
city : New Lisa
city_prefix : Lake
city_suffix : town
color_name : Purple
company : Gray and Sons
company_email : martinallen@johnson.net
company_suffix : and Sons
coordinate : -87.141470
country : Northern Mariana Islands
country_code : SO
credit_card_expire : 12/27
credit_card_full : VISA 19 digit
Adam Murphy
4436102807805944284 02/22
CVC: 607

credit_card_number : 3508735520995723
credit_card_provider : American Express
credit_card_security_code : 129
cryptocurrency : ('BC', 'BlackCoin')
cryptocurrency_code : DRC
cryptocurrency_name : EOS.IO
currency : ('RWF', 'Rwandan franc')
currency_code : CHF
currency_name : São Tomé and Príncipe dobra
date : 2017-03-08
date_between : 2004-09-09
date_between_dates : 2019-01-02
date_object : 2016-06-06
date_of_birth : 1989-05-07
date_this_century : 2016-07-27
date_this_decade : 2015-06-18
date_this_month : 2019-01-01
date_this_year : 2019-01-01
date_time : 1998-05-01 17:23:46
date_time_ad : 0381-09-12 09:49:54
date_time_between : 2004-01-05 11:45:01
date_time_between_dates : 2019-01-02 20:42:43
date_time_this_century : 2008-12-05 18:31:25
date_time_this_decade : 2012-08-18 21:45:49
date_time_this_month : 2019-01-01 22:56:29
date_time_this_year : 2019-01-02 02:34:05
day_of_month : 09
day_of_week : Tuesday
domain_name : mendoza.biz
domain_word : zavala
ean : 4633017886662
ean13 : 1212638242694
ean8 : 00511063
ein : 27-4912026
email : ruizpeter@hotmail.com
file_extension : wav
file_name : close.jpg
file_path : /authority/section.mp4
firefox : Mozilla/5.0 (Windows NT 5.2; mn-MN; rv:1.9.1.20) Gecko/2016-07-12 09:28:12 Firefox/3.6.4
first_name : Brian
first_name_female : Emily
first_name_male : Robert
free_email : toddedward@gmail.com
free_email_domain : yahoo.com
future_date : 2019-01-12
future_datetime : 2019-01-27 11:39:59
get_providers : [<faker.providers.user_agent.Provider object at 0x0000000012F20EF0>, <faker.providers.ssn.en_US.Provider object at 0x0000000012F20E80>, <faker.providers.python.Provider object at 0x0000000012F20E48>, <faker.providers.profile.Provider object at 0x0000000012F20E10>, <faker.providers.phone_number.en_US.Provider object at 0x0000000012F20DD8>, <faker.providers.person.en_US.Provider object at 0x0000000012F20D30>, <faker.providers.misc.Provider object at 0x0000000012F20B00>, <faker.providers.lorem.en_US.Provider object at 0x0000000012F20AC8>, <faker.providers.job.en_US.Provider object at 0x0000000012F20A90>, <faker.providers.isbn.Provider object at 0x0000000012F20A58>, <faker.providers.internet.en_US.Provider object at 0x0000000012F20A20>, <faker.providers.geo.en_US.Provider object at 0x0000000012F209B0>, <faker.providers.file.Provider object at 0x0000000012F20978>, <faker.providers.date_time.en_US.Provider object at 0x0000000012F20940>, <faker.providers.currency.Provider object at 0x0000000012F208D0>, <faker.providers.credit_card.Provider object at 0x0000000012F20898>, <faker.providers.company.en_US.Provider object at 0x0000000012F20860>, <faker.providers.color.en_US.Provider object at 0x0000000012F20828>, <faker.providers.barcode.Provider object at 0x0000000012F207B8>, <faker.providers.bank.en_GB.Provider object at 0x0000000012F20780>, <faker.providers.automotive.en_US.Provider object at 0x0000000012F20518>, <faker.providers.address.en_US.Provider object at 0x0000000012F204A8>]
hex_color : #63efde
hexify : 333f
hostname : srv-88.campbell.com
iban : GB33UOJP7722297851032
image_url : https://placeimg.com/47/982/any
internet_explorer : Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.1)
ipv4 : 192.53.46.149
ipv4_network_class : a
ipv4_private : 10.11.77.107
ipv4_public : 98.36.178.232
ipv6 : 9171:502a:4187:a38b:cab:2ae5:a008:3537
isbn10 : 1-84636-208-3
isbn13 : 978-1-80668-720-6
iso8601 : 1981-11-27T00:10:05
itin : 980-73-6362
job : Environmental health practitioner
language_code : ha
last_name : Harrison
last_name_female : Dean
last_name_male : Washington
latitude : -71.7117395
latlng : (Decimal('-87.243354'), Decimal('68.383566'))
lexify : ynqd
license_plate : 04XI233
linux_platform_token : X11; Linux x86_64
linux_processor : i686
local_latlng : ('30.5427', '-97.54667', 'Hutto', 'US', 'America/Chicago')
locale : tk_TM
location_on_land : ('18.51957', '73.85535', 'Pune', 'IN', 'Asia/Kolkata')
longitude : -149.969067
mac_address : f7:42:08:e8:c9:a5
mac_platform_token : Macintosh; PPC Mac OS X 10_6_3
mac_processor : U; Intel
md5 : 7e7cb2e1d96aa3ea8af015bd7d21ae3c
military_apo : PSC 3000, Box 1879
military_dpo : Unit 8330 Box 5496
military_ship : USNV
military_state : AA
mime_type : multipart/signed
month : 07
month_name : August
msisdn : 3162686718629
name : Melissa Gonzalez
name_female : Annette Norton
name_male : Justin Martinez
null_boolean : True
numerify : 110
opera : Opera/8.67.(X11; Linux i686; mai-IN) Presto/2.9.166 Version/10.00
paragraph : Less huge partner herself. Major career personal sort western impact.
paragraphs : [u'Tonight else fear doctor. Radio past event garden.', u'Series technology section capital hundred go choice.', u'Large month resource concern Mr form machine. Simple site however watch.']
password : _NJnqyB+o8
past_date : 2018-12-03
past_datetime : 2018-12-29 23:03:20
phone_number : 001-810-068-3558x30683
postalcode : 63294
postalcode_plus4 : 24817-9240
postcode : 36965
prefix : Miss
prefix_female : Miss
prefix_male : Mr.
profile : {'website': [u'http://knox.com/', u'https://hall-james.com/', u'https://gibbs.com/', u'http://gonzalez-pacheco.biz/'], 'username': u'hensonana', 'name': u'Timothy Strickland', 'blood_group': 'A+', 'residence': u'50459 April Dale\nNorth Jasmine, IL 92057', 'company': u'Hays, Mcdonald and Costa', 'address': u'9407 Donna Summit\nAnnetteville, WY 18513', 'birthdate': datetime.date(1980, 12, 19), 'sex': 'M', 'job': 'Engineer, site', 'ssn': u'170-61-7134', 'current_location': (Decimal('-42.5555445'), Decimal('42.848347')), 'mail': u'janetwalsh@gmail.com'}
pybool : False
pydecimal : -88679.375
pydict : {u'try': u'arthurday@hotmail.com', u'attorney': u'https://davis-shepherd.org/', u'beyond': u'AHMeTAiybSObDygFLCdP', u'make': Decimal('8.9'), u'sign': u'ugHKkPrnstWEoDyEWvmx', u'should': 9959, u'project': Decimal('-924424356.274'), u'born': u'kenHkltRzSJhjBZmvRaT', u'oil': u'xCFyZuRlAKZrvYkpSqxg', u'each': Decimal('61.99'), u'arrive': u'dHyljteKscrRHmswyaZU', u'minute': u'uZRBiOfKafHJcaaKXZXM'}
pyfloat : -7651023230.8
pyint : 3534
pyiterable : [u'VHLKqNIghXURLCTtpzRn', Decimal('6012.9444191'), -35834818801.4104, 6171, Decimal('-12265352.2901'), 3595, u'https://www.salinas.biz/category.php', u'tmullen@harris.biz', datetime.datetime(2014, 6, 24, 0, 55, 42), -93.696436254134, u'NHntgKLcrlNkmEzlgNAR']
pylist : [u'jose07@yahoo.com', u'BgAbucmgaDtzDNkWHZxA', u'BGHXEbOTzWesohqDvSvd', datetime.datetime(1996, 7, 15, 11, 51, 9), u'rWVgMmfqZPaHMvIKXqbR', u'zachary98@yahoo.com', u'aPYhQZYdfMWLbegYpTrX', u'LBlIvbwnXKmfLscVFmyu', u'http://www.brown.com/homepage/', 2009, u'terri30@stafford-barnett.biz']
pyset : set([u'vtURKBQQRApIoUZROrll', 1105, u'jalvarado@gmail.com', u'QmqPChxAQibTqClxJhuD', datetime.datetime(2002, 8, 15, 23, 1, 56), Decimal('6.968001051'), 3837655878192.6, u'benjaminholloway@clark.com', -6306.918624, 3190, Decimal('-25374593628.5'), u'lgnXmYBtXNzCKpVvCYrb', Decimal('-33897542.763')])
pystr : LqxqCNVABxSlGFNkvrlx
pystruct : ([u'jjkLtZqEljABLAkzTfUt', datetime.datetime(2008, 10, 8, 1, 50, 36), u'ZjEpHkuuMBAZkaBhQyrU', u'OilUAUmcThgkcxMbqSAh', datetime.datetime(1975, 7, 5, 0, 36, 55), u'fOJrQchzyPhFxqMdGMnE', 9250, 8949, u'sleWkpsNBMihVkGpRoot', Decimal('807164941.401')], {u'begin': u'BjbPTjfUvVfKcGiJMNQD', u'necessary': u'FzOHQMomtEMTgadZmixE', u'show': Decimal('53203338.7976'), u'country': u'swfMuAlLcqRLksRZwlyU', u'understand': u'lisalewis@mckay.org', u'rock': 9670.5514, u'close': 5565, u'east': 102813994221205.0, u'my': u'yPyrXsPeXixbldFbWhFU', u'those': u'efry@hotmail.com'}, {u'product': {4: u'patrickparker@hotmail.com', 5: [u'qCHqzUfasGcUMKjUhkLc', u'FxwucZAfcnYxrhqOOtXR', -395.803], 6: {4: u'aLqAwymVaNpmhyHhQOLH', 5: u'ErfioxhgoWNVCdnaVanq', 6: [u'RlnaSIsbWnKfaPViBLNj', u'jbTTgCCjCEtuzGodHAdV']}}, u'could': {0: u'RgwNMAMzXKlHhazdfhCX', 1: [u'EAUsPCgHgoQDJcblUCja', datetime.datetime(2012, 1, 19, 13, 35, 8), u'http://walters.org/explore/wp-content/app/author.htm'], 2: {0: u'lwPTuQsvNnCUPBBTffno', 1: u'eXQMtXfRtlBJfpVFAUjg', 2: [u'ECOKoifYhSzLoifZdWnl', u'https://johnson.net/category.html']}}, u'term': {8: datetime.datetime(2010, 1, 9, 12, 24, 49), 9: [datetime.datetime(2005, 11, 13, 14, 10, 18), 6119, u'rzsUSoLRotLlQSjzaskb'], 10: {8: u'belinda84@hotmail.com', 9: datetime.datetime(1997, 3, 10, 4, 46, 15), 10: [Decimal('663.0'), Decimal('6.83132854674E+13')]}}, u'environment': {1: u'qbWPXDBMWzHZELWxQBuz', 2: [6074, 3400, u'eHYTsFimBxNJryhThDNc'], 3: {1: datetime.datetime(1984, 4, 17, 4, 38, 31), 2: u'VhigpvUUAfCHutXcwfbX', 3: [u'UAjJhtniSwdhayTOLWEq', u'NrYZWiQYFJZMYmxiRLww']}}, u'rich': {9: u'UxqyzUuWWYhlsbfWtBPa', 10: [u'VvsHEFsWBmxlVzeCKMIX', 4015, datetime.datetime(1987, 5, 8, 6, 28, 31)], 11: {9: datetime.datetime(1994, 12, 29, 22, 3, 29), 10: u'IeVrPuRMbVQPQLpnXodo', 11: [u'YKUAepdkQNykJLtiEmeH', Decimal('86832605439.3')]}}, u'into': {8: [u'WFkKXgCihEjoMxPMpKiB', -371.213906693373, u'dbrowning@yahoo.com'], 9: {8: u'melissa96@hotmail.com', 9: [u'adamstammy@hotmail.com', 4613], 7: Decimal('-870.723488439')}, 7: u'https://anderson.com/'}, u'skill': {5: u'ZcQKPbNrKTKerNTBcOUu', 6: [1294, Decimal('532156.674'), 247], 7: {5: u'ZDVnfVsiIkWhZRhwhElc', 6: 3777, 7: [u'basssusan@barnett.com', u'gTRgZifCkCkKVgAoSuqm']}}, u'democratic': {2: u'https://gonzalez-dean.com/', 3: [u'millerrachel@schneider.com', u'FkpbQxHdbGinsxifzjLo', u'IPhyirwsceVwTKNpduNW'], 4: {2: u'AOOVvMWWxDyWBFYqZPZq', 3: u'PHdenfFfHQzsHrqmFFNJ', 4: [datetime.datetime(2006, 12, 10, 10, 21, 14), 7330]}}, u'themselves': {8: {8: [u'william87@yahoo.com', u'YRoBtIsJwgGXcuvpIJPj'], 6: 1245, 7: u'ygriffin@smith-rowe.com'}, 6: u'jefferyhall@yahoo.com', 7: [u'qherrera@hunt.info', u'andrewharvey@gmail.com', u'http://brown.info/list/wp-content/author/']}, u'today': {3: u'btSTHtwKXrLHvLpFKOVZ', 4: [u'https://www.ramirez.com/faq.php', u'dpsAElcgRCbovSDyVvdd', u'https://mcintosh-swanson.com/main/search/tag/terms/'], 5: {3: 9055, 4: datetime.datetime(1971, 7, 28, 12, 33, 9), 5: [u'danielhodges@fisher.com', u'EfuYhbcQKCQcptDTlmhM']}}})
random_choices : ['c']
random_digit : 6
random_digit_not_null : 8
random_digit_not_null_or_empty : 6
random_digit_or_empty : 
random_element : b
random_elements : ['a']
random_int : 7153
random_letter : v
random_letters : ['u', 'e', 'a', 'y', 'E', 'A', 'm', 'R', 'k', 'm', 'G', 'o', 'q', 's', 'l', 'O']
random_lowercase_letter : c
random_number : 60
random_sample : ['b', 'a', 'c']
random_uppercase_letter : E
randomize_nb_elements : 13
rgb_color : 207,33,21
rgb_css_color : rgb(141,14,63)
safari : Mozilla/5.0 (Windows; U; Windows NT 6.0) AppleWebKit/535.12.3 (KHTML, like Gecko) Version/5.0 Safari/535.12.3
safe_color_name : olive
safe_email : martinkayla@example.org
safe_hex_color : #bb2200
secondary_address : Suite 437
seed : None
seed_instance : <faker.generator.Generator object at 0x000000001280D7F0>
sentence : Couple race sit sing TV.
sentences : [u'Surface seem serve five each magazine.', u'Statement current line share force prevent company.', u'Daughter listen fear miss.']




自定义faker,用add_provider方法:

from faker import Faker
from faker.providers import BaseProvider
import random

class MyProvider(BaseProvider):
    def foo(self):
        return  random.random()

fake = Faker()
fake.add_provider(MyProvider)
print fake.foo()

其它Provider参考: https://faker.readthedocs.io/en/latest/providers.html

posted @ 2019-01-02 20:22  泽锦  阅读(3331)  评论(0编辑  收藏  举报