Python faker 生成测试数据

pip install Faker

faker官网

基本使用

python 复制代码
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

怎么生成中文数据?

python 复制代码
fake = Faker('zh_CN')

多国语言数据

python 复制代码
fake=Faker(['it_IT', 'en_US', 'ja_JP','zh_CN'])

支持那些国家的语言?

python 复制代码
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']

支持生成那些数据?

python 复制代码
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

自定义假数据

方法一

python 复制代码
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())

方法二

python 复制代码
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())

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

相关推荐
码农张思壮27 分钟前
使用 AI 编码将路书 GPX 文件导入高德地图生成路线的尝试
后端·python·openai
又南又难38 分钟前
deepFM模型pytorch实现
人工智能·pytorch·python
bst@微胖子1 小时前
Python实现接口签名调用
android·java·python
佛曰我不想说话2 小时前
简洁安装配置在Windows环境下使用vscode开发pytorch
pytorch·python
摸鱼仙人~2 小时前
pytorch中nn.Conv2d详解及参数设置原则
人工智能·pytorch·python
绍兴贝贝3 小时前
代码随想录算法训练营第五十天|图论基础|深度优先搜索理论基础|KM98.所有可达路径|广度优先搜索理论基础
数据结构·人工智能·python·算法·力扣
羊小猪~~3 小时前
基于RNN模型的心脏病预测(tensorflow实现)
人工智能·python·rnn·深度学习·神经网络·机器学习·tensorflow
feifeikon3 小时前
TensorFlow DAY1:基础语法
人工智能·python·tensorflow
懒大王爱吃狼3 小时前
python基于diagrams库绘制系统架构图
开发语言·python·系统架构·自动化·python基础·python教程
JavaPub-rodert3 小时前
项目48:简易语言学习助手【源代码】 --- 《跟着小王学Python·新手》
服务器·开发语言·python·学习·microsoft