Faker - 测试数据生成器
安装
pip install faker
# or
pip3 install faker
使用
包含
from faker import Faker
初始化
fake=Faker(locale='zh_CN')
生成数据
个人信息
fake.name() # 名字
# '常鹏'
fake.job() # 职位
# '美容师'
fake.address() # 地址
# '江苏省佛山市闵行赵街k座 627653'
fake.ssn(min_age=18, max_age=90) # 身份证
# '360982197809090811'
fake.msisdn() # 完整手机号码(加了国家和国内区号)
# '1429208504203'
fake.phone_number() # 手机号
# '15689331742'
fake.phonenumber_prefix() # 区号
# 157
fake.license_plate() # 汽车牌照
# '8D Q1196'
fake.bank_country() # 银行所属国家
# 'GB'
fake.bban() # 基本银行账号
# 'TPET9323218579379'
fake.iban() # 国际银行代码
# 'GB82IRVM1531009974701'
fake.credit_card_expire(start="now", end="+10y", date_format="%m/%y") # 过期年月
# '08/28'
fake.credit_card_full(card_type=None) # 完整信用卡信息
# 'Discover\n小红 彭\n6011256963504297 09/28\nCVC: 005\n'
fake.credit_card_number(card_type=None) # 信用卡卡号
# '4339481813664365360'
fake.credit_card_provider(card_type=None) # 信用卡提供商
# 'American Express'
fake.credit_card_security_code(card_type=None) # 信用卡安全码
# '287'
fake.profile(fields=None, sex=None) # 档案(完整)
fake.simple_profile(sex=None) # 档案(简单)
时间
fake.am_pm() # AM或PM
# 'PM'
fake.century() # 世纪
# 'XII'
fake.date(pattern="%Y-%m-%d", end_datetime=None) # 日期字符串(可设置格式和最大日期)
# '1998-05-13'
fake.date_between(start_date="-30y", end_date="today") # 日期(可设置限定范围)
# datetime.date(2014, 8, 17)
fake.date_between_dates(date_start=None, date_end=None) # 同上
# datetime.date(2019, 10, 14)
fake.date_object(end_datetime=None) # 日期(可设置最大日期)
# datetime.date(1981, 12, 20)
fake.date_of_birth(tzinfo=None, minimum_age=0, maximum_age=115) # 出生日期
# datetime.date(1931, 12, 8)
fake.date_this_century(before_today=True, after_today=False) # 本世纪日期
# datetime.date(2003, 5, 4)
fake.date_this_decade(before_today=True, after_today=False) # 本年代中的日期
# datetime.date(2014, 1, 29)
fake.date_this_month(before_today=True, after_today=False) # 本月中的日期
# datetime.date(2019, 10, 10)
fake.date_this_year(before_today=True, after_today=False) # 本年中的日期
# datetime.date(2019, 3, 6)
fake.date_time(tzinfo=None, end_datetime=None) # 日期和时间
# datetime.datetime(1990, 8, 11, 22, 25)
fake.date_time_ad(tzinfo=None, end_datetime=None, start_datetime=None) # 日期和时间(从001年1月1日到现在)
# datetime.datetime(244, 12, 17, 9, 59, 56)
fake.date_time_between(start_date="-30y", end_date="now", tzinfo=None) # 日期时间(可设置限定范围)
# datetime.datetime(1995, 4, 19, 17, 23, 51)
fake.date_time_between_dates(datetime_start=None, datetime_end=None, tzinfo=None) # 同上
# datetime.datetime(2019, 10, 14, 14, 15, 36)
fake.date_time_this_century(before_now=True, after_now=False, tzinfo=None) # 本世纪中的日期和时间
# datetime.datetime(2009, 8, 26, 18, 27, 9)
fake.date_time_this_decade(before_now=True, after_now=False, tzinfo=None) # 本年代中的日期和时间
# datetime.datetime(2019, 2, 24, 22, 18, 44)
fake.date_time_this_month(before_now=True, after_now=False, tzinfo=None) # 本月中的日期和时间
# datetime.datetime(2019, 10, 3, 9, 20, 44)
fake.date_time_this_year(before_now=True, after_now=False, tzinfo=None) # 本年中的日期和时间
# datetime.datetime(2019, 2, 10, 7, 3, 18)
fake.day_of_month() # 几号
# '23'
fake.day_of_week() # 星期几
# 'Tuesday'
fake.future_date(end_date="+30d", tzinfo=None) # 未来日期
# datetime.date(2019, 10, 28)
fake.future_datetime(end_date="+30d", tzinfo=None) # 未来日期和时间
# datetime.datetime(2019, 10, 28, 21, 4, 35)
fake.iso8601(tzinfo=None, end_datetime=None) # iso8601格式日期和时间
# '1995-04-10T00:45:01'
fake.month() # 第几月
# '07'
fake.month_name() # 月份名称
# 'December'
fake.past_date(start_date="-30d", tzinfo=None) # 过去日期
# datetime.date(2019, 10, 3)
fake.past_datetime(start_date="-30d", tzinfo=None) # 过去日期和时间
# datetime.datetime(2019, 9, 30, 20, 25, 43)
fake.time(pattern="%H:%M:%S", end_datetime=None) # 时间(可设置格式和最大日期时间)
# '14:26:44'
fake.time_delta(end_datetime=None) # 时间间隔
# datetime.timedelta(0)
fake.time_object(end_datetime=None) # 时间(可设置最大日期时间)
# datetime.time(4, 41, 39)
fake.time_series(start_date="-30d", end_date="now", precision=None, distrib=None, tzinfo=None)
# <generator object Provider.time_series at 0x7fadf51e0930>
fake.timezone() # 时区
# 'Asia/Baku'
fake.unix_time(end_datetime=None, start_datetime=None) # UNIX时间戳
# 393980728
fake.year() # 某年
# '2016'
文件
fake.file_extension(category=None) # 文件扩展名
# 'avi'
fake.file_name(category=None, extension=None) # 文件名
# '专业.pptx'
fake.file_path(depth=1, category=None, extension=None) # 文件路径
# '/的话/以上.ods'
fake.mime_type(category=None) # MIME类型
# 'application/xop+xml'
fake.unix_device(prefix=None) # UNIX设备
# '/dev/xvdq'
fake.unix_partition(prefix=None) # UNIX分区
# '/dev/xvdc6'
地址
fake.address() # 地址
# '香港特别行政区大冶县上街钟街k座 664713'
fake.building_number() # 楼名
# 'v座'
fake.city() # 完整城市名
# '长春县'
fake.city_name() # 城市名字(不带市县)
# '梧州'
fake.city_suffix() # 城市后缀名
# '市'
fake.country() # 国家名称
# '厄立特里亚'
fake.country_code(representation="alpha-2")
# 'BZ' # 国家编号
fake.district() # 地区
# '沙湾'
fake.postcode() # 邮编
# '332991'
fake.province() # 省
# '河北省'
fake.street_address() # 街道地址
# '武汉街D座'
fake.street_name() # 街道名称
# '广州路'
fake.street_suffix() # 街道后缀名
# '路'
条形码
fake.ean(length=13) # EAN条形码
# '0092153312321'
fake.ean13() # EAN13条形码
# '5449769874395'
fake.ean8() # EAN8条形码
# '12152148'
颜色
fake.color_name() # 颜色名称
# 'Orange'
fake.hex_color() # 颜色十六进制值
# '#a5cb7c'
fake.rgb_color() # 颜色RGB值
# '15,245,42'
fake.rgb_css_color() # CSS颜色值
# 'rgb(15,70,13)'
fake.safe_color_name() # 安全色
# 'aqua'
fake.safe_hex_color() # 安全色十六进制值
# '#881100'
货币
fake.cryptocurrency() # 加密货币代码+名称
# ('BCH', 'Bitcoin Cash')
fake.cryptocurrency_code() # 加密货币代码
# 'MSC'
fake.cryptocurrency_name() # 加密货币名称
# 'SwiftCoin'
fake.currency() # 货币代码+名称
# ('NIO', 'Nicaraguan córdoba')
fake.currency_code() # 货币代码
# 'VEF'
fake.currency_name() # 货币名称
# 'Australian dollar'
公司
fake.bs() # 商业用词
# 'synthesize strategic vortals'
fake.catch_phrase() # 妙句(口号)
# 'Robust even-keeled service-desk'
fake.company() # 公司名称
# '富罳科技有限公司'
fake.company_prefix() # 公司名称前缀
# '商软冠联'
fake.company_suffix() # 公司名称后缀
# '网络有限公司'
地理坐标
fake.coordinate(center=None, radius=0.001) # 坐标
# Decimal('147.543284')
fake.latitude() # 纬度
# Decimal('66.519139')
fake.latlng() # 经纬度
# (Decimal('55.3370965'), Decimal('-15.427896'))
fake.local_latlng(country_code="US", coords_only=False) # 返回某个国家某地的经纬度
# ('25.67927', '-80.31727', 'Kendall', 'US', 'America/New_York')
fake.location_on_land(coords_only=False) # 返回地球上某个位置的经纬度
# ('42.50729', '1.53414', 'les Escaldes', 'AD', 'Europe/Andorra')
fake.longitude() # 经度
# Decimal('70.815233')
邮箱
fake.ascii_company_email(*args, **kwargs) # 企业邮箱(ascii编码)
# 'qiuyan@xiulan.cn'
fake.ascii_email(*args, **kwargs) # 企业邮箱+免费邮箱(ascii编码)
# 'lei59@78.net'
fake.ascii_free_email(*args, **kwargs) # 免费邮箱(ascii编码)
# 'pcheng@gmail.com'
fake.ascii_safe_email(*args, **kwargs) # 安全邮箱(ascii编码)
# 'fangyan@example.org'
fake.company_email(*args, **kwargs) # 企业邮箱
# 'scao@pingjing.net'
fake.domain_name(levels=1) # 域名
# 'dy.cn'
fake.domain_word(*args, **kwargs) # 二级域名
# 'gangxiuying'
fake.email(*args, **kwargs) # 企业邮箱+免费邮箱
# 'na13@ding.cn'
fake.free_email(*args, **kwargs) # 免费邮箱
# 'fang48@hotmail.com'
fake.free_email_domain(*args, **kwargs) # 免费邮箱域名
# 'yahoo.com'
fake.hostname(*args, **kwargs) # 主机名
# 'lt-70.53.cn'
网络
fake.image_url(width=None, height=None) # 图片URL
# 'https://placekitten.com/752/243'
fake.ipv4(network=False, address_class=None, private=None) # ipv4
# '160.152.149.78'
fake.ipv4_network_class() # ipv4网络等级
# 'b'
fake.ipv4_private(network=False, address_class=None) # 私有ipv4
# '10.99.124.57'
fake.ipv4_public(network=False, address_class=None) # 公共ipv4
# '169.120.29.235'
fake.ipv6(network=False) # ipv6
# 'f392:573f:d60f:9aed:2a4c:36d7:fe5b:7034'
fake.mac_address() # MAC地址
# '62:67:79:8c:c2:40'
fake.safe_email(*args, **kwargs) # 安全邮箱
# 'jing58@example.org'
fake.slug(*args, **kwargs) # URL中的slug
# ''
fake.tld() # 顶级域名
# 'cn'
fake.uri() # URI
# 'http://yi.com/list/main/explore/register.php'
fake.uri_extension() # URI扩展
# '.php'
fake.uri_page() # URI页
# 'terms'
fake.uri_path(deep=None) # URI路径
# 'blog/tags/blog'
fake.url(schemes=None) # URL
# 'http://liutao.cn/'
fake.user_name(*args, **kwargs) # 用户名
# 'xiulan80'
书籍
fake.isbn10(separator="-") # ISBN-10图书编号
# '0-588-73943-X'
fake.isbn13(separator="-") # ISBN-13图书编号
# '978-1-116-51399-8'
文本
fake.paragraph(nb_sentences=3, variable_nb_sentences=True, ext_word_list=None) # 单个段落
# '一样不会因此作者.注意法律拥有广告查看.标题以及设备有限责任资源因此.'
fake.paragraphs(nb=3, ext_word_list=None) # 多个段落
# ['...', '...', '...']
fake.sentence(nb_words=6, variable_nb_words=True, ext_word_list=None) # 单个句子
# '处理制作最大销售都是社区不断..'
fake.sentences(nb=3, ext_word_list=None) # 多个句子
# ['....', '....', '....']
fake.text(max_nb_chars=200, ext_word_list=None) # 单个文本
# ...
fake.texts(nb_texts=2, max_nb_chars=200, ext_word_list=None) # 多个文本
# ["...","..."]
fake.word(ext_word_list=None) # 单个词语
# '直接'
fake.words(nb=3, ext_word_list=None, unique=False) # 多个词语
# ['产品', '参加', '法律']
User-Agent
fake.android_platform_token() # 安卓
# 'Android 5.0.1'
fake.chrome(version_from=13, version_to=63, build_from=800, build_to=899) # Chrome
# ('Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_10_9) AppleWebKit/534.0 (KHTML, '
# 'like Gecko) Chrome/62.0.826.0 Safari/534.0')
fake.firefox() # FireFox
# ('Mozilla/5.0 (Windows NT 5.1; cs-CZ; rv:1.9.0.20) Gecko/2010-12-02 06:14:30 '
# 'Firefox/3.6.5')
fake.internet_explorer() # Ie
# 'Mozilla/5.0 (compatible; MSIE 7.0; Windows 95; Trident/3.0)'
fake.ios_platform_token() # ios
# 'iPhone; CPU iPhone OS 5_1_1 like Mac OS X'
fake.linux_platform_token() # Linux
# 'X11; Linux i686'
fake.linux_processor() # Linux处理器
# 'x86_64'
fake.mac_platform_token() # Mac
# 'Macintosh; U; PPC Mac OS X 10_11_2'
fake.mac_processor() # Mac处理器
# 'Intel'
fake.opera() # Opera
# 'Opera/8.32.(Windows 98; Win 9x 4.90; mr-IN) Presto/2.9.188 Version/10.00'
fake.safari() # Safari
# ('Mozilla/5.0 (Windows; U; Windows NT 6.0) AppleWebKit/533.43.6 (KHTML, like '
# 'Gecko) Version/4.0.5 Safari/533.43.6')
fake.user_agent() # 随机用户代理
# 'Mozilla/5.0 (compatible; MSIE 9.0; Windows 95; Trident/3.0)'
fake.windows_platform_token() # Windows
# 'Windows NT 6.2'
附录
支持的区域见下表
地区代码 | 释义 |
---|---|
ar_EG | Arabic (Egypt) |
ar_PS | Arabic (Palestine) |
ar_SA | Arabic (Saudi Arabia) |
bg_BG | Bulgarian |
cs_CZ | Czech |
de_CH | Swiss Standard German |
de_DE | German |
dk_DK | Danish |
el_GR | Greek |
en_AU | English (Australia) |
en_CA | English (Canada) |
en_GB | English (Great Britain) |
en_US | English (United States) |
es_ES | Spanish (Spain) |
es_MX | Spanish (Mexico) |
et_EE | Estonian |
fa_IR | Persian (Iran) |
fi_FI | Finnish |
fr_FR | French |
fr_CA | French(Canada) |
fr_CH | French(Switzerland) |
hi_IN | Hindi |
hr_HR | Croatian |
hu_HU | Hungarian |
id_ID | Indonesia |
it_IT | Italian |
ja_JP | Japanese |
ko_KR | Korean |
lt_LT | Lithuanian |
lv_LV | Latvian |
ne_NP | Nepali |
nl_NL | Dutch (Netherlands) |
no_NO | Norwegian |
or_IN | India Oriya |
pl_PL | Polish |
pt_BR | Portuguese (Brazil) |
pt_PT | Portuguese (Portugal) |
ro_RO | Romanian |
ru_RU | Russian |
sl_SI | Slovene |
sv_SE | Swedish |
tr_TR | Turkish |
uk_UA | Ukrainian |
zh_CN | Chinese (China) |
zh_TW | Chinese (Taiwan) |