Python faker 生成测试数据

pip install Faker

faker官网

基本使用

from faker import Faker

if __name__ == '__main__':
    fake = Faker()
    print(fake.name())
    print(fake.user_name())
    print(fake.phone_number())
    print(fake.address())
# 输出:
Jorge Campbell
grobbins
769.415.0562x2019
255 Brooks Parkways
Gibbshaven, MO 43247

默认是英文 en_US

怎么生成中文数据?

fake = Faker('zh_CN')

多国语言数据

fake=Faker(['it_IT', 'en_US', 'ja_JP','zh_CN'])

支持那些国家的语言?

from faker.config import AVAILABLE_LOCALES

print(AVAILABLE_LOCALES)

# ['ar_AA', 'ar_AE', 'ar_BH', 'ar_EG', 'ar_JO', 'ar_PS', 'ar_SA', 'az_AZ', 'bg_BG', 'bn_BD', 'bs_BA', 'cs_CZ', 'da_DK', 'de', 'de_AT', 'de_CH', 'de_DE', 'dk_DK', 'el_CY', 'el_GR', 'en', 'en_AU', 'en_CA', 'en_GB', 'en_IE', 'en_IN', 'en_NZ', 'en_PH', 'en_TH', 'en_US', 'es', 'es_AR', 'es_CA', 'es_CL', 'es_CO', 'es_ES', 'es_MX', 'et_EE', 'fa_IR', 'fi_FI', 'fil_PH', 'fr_BE', 'fr_CA', 'fr_CH', 'fr_FR', 'fr_QC', 'ga_IE', 'he_IL', 'hi_IN', 'hr_HR', 'hu_HU', 'hy_AM', 'id_ID', 'it_CH', 'it_IT', 'ja_JP', 'ka_GE', 'ko_KR', 'la', 'lb_LU', 'lt_LT', 'lv_LV', 'mt_MT', 'ne_NP', 'nl_BE', 'nl_NL', 'no_NO', 'or_IN', 'pl_PL', 'pt_BR', 'pt_PT', 'ro_RO', 'ru_RU', 'sk_SK', 'sl_SI', 'sq_AL', 'sv_SE', 'ta_IN', 'th', 'th_TH', 'tl_PH', 'tr_TR', 'tw_GH', 'uk_UA', 'vi_VN', 'zh_CN', 'zh_TW', 'zu_ZA']

支持生成那些数据?

from faker import Faker

if __name__ == '__main__':
    fake = Faker()
    for data in dir(fake):
        if data.startswith('_'):
            continue
        print(data, end=', ')

# aba, add_provider, address, administrative_unit, am_pm, android_platform_token, ascii_company_email, ascii_email, ascii_free_email, ascii_safe_email, bank_country, basic_phone_number, bban, binary, boolean, bothify, bs, building_number, cache_pattern, catch_phrase, century, chrome, city, city_prefix, city_suffix, color, color_name, company, company_email, company_suffix, coordinate, country, country_calling_code, country_code, credit_card_expire, credit_card_full, credit_card_number, credit_card_provider, credit_card_security_code, cryptocurrency, cryptocurrency_code, cryptocurrency_name, csv, currency, currency_code, currency_name, currency_symbol, current_country, current_country_code, date, date_between, date_between_dates, date_object, date_of_birth, date_this_century, date_this_decade, date_this_month, date_this_year, date_time, date_time_ad, date_time_between, date_time_between_dates, date_time_this_century, date_time_this_decade, date_time_this_month, date_time_this_year, day_of_month, day_of_week, del_arguments, dga, domain_name, domain_word, dsv, ean, ean13, ean8, ein, email, emoji, enum, factories, file_extension, file_name, file_path, firefox, first_name, first_name_female, first_name_male, first_name_nonbinary, fixed_width, format, free_email, free_email_domain, future_date, future_datetime, generator_attrs, get_arguments, get_formatter, get_providers, hex_color, hexify, hostname, http_method, iana_id, iban, image, image_url, internet_explorer, invalid_ssn, ios_platform_token, ipv4, ipv4_network_class, ipv4_private, ipv4_public, ipv6, isbn10, isbn13, iso8601, items, itin, job, json, json_bytes, language_code, language_name, last_name, last_name_female, last_name_male, last_name_nonbinary, latitude, latlng, lexify, license_plate, linux_platform_token, linux_processor, local_latlng, locale, locales, localized_ean, localized_ean13, localized_ean8, location_on_land, longitude, mac_address, mac_platform_token, mac_processor, md5, military_apo, military_dpo, military_ship, military_state, mime_type, month, month_name, msisdn, name, name_female, name_male, name_nonbinary, nic_handle, nic_handles, null_boolean, numerify, opera, optional, paragraph, paragraphs, parse, passport_dates, passport_dob, passport_full, passport_gender, passport_number, passport_owner, password, past_date, past_datetime, phone_number, port_number, postalcode, postalcode_in_state, postalcode_plus4, postcode, postcode_in_state, prefix, prefix_female, prefix_male, prefix_nonbinary, pricetag, profile, provider, providers, psv, pybool, pydecimal, pydict, pyfloat, pyint, pyiterable, pylist, pyobject, pyset, pystr, pystr_format, pystruct, pytimezone, pytuple, random, random_choices, random_digit, random_digit_above_two, random_digit_not_null, random_digit_not_null_or_empty, random_digit_or_empty, random_element, random_elements, random_int, random_letter, random_letters, random_lowercase_letter, random_number, random_sample, random_uppercase_letter, randomize_nb_elements, rgb_color, rgb_css_color, ripe_id, safari, safe_color_name, safe_domain_name, safe_email, safe_hex_color, sbn9, secondary_address, seed, seed_instance, seed_locale, sentence, sentences, set_arguments, set_formatter, sha1, sha256, simple_profile, slug, ssn, state, state_abbr, street_address, street_name, street_suffix, suffix, suffix_female, suffix_male, suffix_nonbinary, swift, swift11, swift8, tar, text, texts, time, time_delta, time_object, time_series, timezone, tld, tsv, unique, unix_device, unix_partition, unix_time, upc_a, upc_e, uri, uri_extension, uri_page, uri_path, url, user_agent, user_name, uuid4, vin, weights, windows_platform_token, word, words, xml, year, zip, zipcode, zipcode_in_state, zipcode_plus4

自定义假数据

方法一

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


class MyProvider(BaseProvider):
    def my_fack_data(self) -> str:
        samples = ['自定义数据1', '自定义数据2']
        return choice(samples)


if __name__ == '__main__':
    fake = Faker()
    fake.add_provider(MyProvider)
    print(fake.my_fack_data())

方法二

from faker import Faker
from faker.providers import DynamicProvider

medical_professions_provider = DynamicProvider(
    provider_name="medical_profession",
    elements=["dr.", "doctor", "nurse", "surgeon", "clerk"],
)

if __name__ == '__main__':
    fake = Faker()
    fake.add_provider(medical_professions_provider)

    print(fake.medical_profession())

基本够用了,更多操作见官网例子

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值