Home > Backend Development > Python Tutorial > Tips | A Python library that looks so fake that it cracked me up. .

Tips | A Python library that looks so fake that it cracked me up. .

Release: 2023-08-10 14:51:30
forward
1116 people have browsed it


When I was browsing GitHub today, I found a very cool library-- Faker, this library can generate some false information, including personal information or some test data, if we need to use some false data during development Test, then this library will come in handy~

1. Installation

##Just install with pip:
pip install Faker
Copy after login

2. 使用

2.1 简单使用

创建虚拟姓名、电话号码、地址等信息:

from faker import Faker

fake = Faker()

fake.name()
# 'Jonathon Dixon'

fake.phone_number()
# '262-035-1927'

fake.address()
# '590 Hart Motorway\nSergioshire, NM 76453'
Copy after login

2.2 国内信息

这个库默认显示是国外的信息,接下来我们设置一下国内信息
from faker import Faker

fake = Faker('zh_CN')

fake.name()
# '汪雪梅'

fake.phone_number()
# '18535612607'

fake.address()
# '重庆市秀云县静安潜江街X座 690499'
Copy after login

是不是可以假乱真~

当然,如果要生成其他语种或地区的数据,也是可以的,直接替换Faker参数即可,以下是这个库所支持的语种:
ar_EG - Arabic (Egypt)
ar_PS - Arabic (Palestine)
ar_SA - Arabic (Saudi Arabia)
bg_BG - Bulgarian
bs_BA - Bosnian
cs_CZ - Czech
de_DE - German
dk_DK - Danish
el_GR - Greek
en_AU - English (Australia)
en_CA - English (Canada)
en_GB - English (Great Britain)
en_NZ - English (New Zealand)
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
hi_IN - Hindi
hr_HR - Croatian
hu_HU - Hungarian
hy_AM - Armenian
it_IT - Italian
ja_JP - Japanese
ka_GE - Georgian (Georgia)
ko_KR - Korean
lt_LT - Lithuanian
lv_LV - Latvian
ne_NP - Nepali
nl_NL - Dutch (Netherlands)
no_NO - Norwegian
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 Mainland)
zh_TW - Chinese (China Taiwan)
Copy after login

2.3 生成个人档案信息

fake.profile(fields=None, sex=None)
Copy after login
结果:
Tips | A Python library that looks so fake that it cracked me up. .


2.4 批量生成人员信息

import collections
import ngender
import datetime
import pandas as pd

all_info = []
pa_list = ['name','gender', 'age', 'job', 'company', 'address', 'phone_number', 'company_email']
for i in range(15):
    people = collections.namedtuple('User', pa_list) 
    people.name = fake.name()
    people.gender = '男' if ngender.guess(people.name)[0] == 'male' else '女'
    people.age = datetime.datetime.now().year - fake.date_of_birth(tzinfo=None, minimum_age=25, maximum_age=40).year # 出生日期
    people.job = fake.job()
    people.company = fake.company()
    people.address = fake.address().split(' ')[0]
    people.phone_number = fake.phone_number()
    people.company_email = fake.company_email()
    lsts = [people.name, people.gender, people.age, people.job, people.company, people.address, people.phone_number, people.company_email]
    all_info.append(lsts)

pd.DataFrame(all_info,columns=pa_list)
Copy after login
效果:

Tips | A Python library that looks so fake that it cracked me up. .

fake 实例还有很多方法可用:
  • address 地址

  • person 人物类

  • barcode 条码类

  • color 颜色类

  • company 公司类

  • credit_card 银行卡类

  • currency 货币

  • date_time 时间日期类

  • file 文件类

  • internet 互联网类

  • job 工作

  • lorem 乱数假文

  • misc 杂项类

  • phone_number 手机号

  • python python数据

  • profile 档案信息

  • ssn 身份证号码

  • user_agent 用户代理

官方文档:

https://faker.readthedocs.io/en/master/

The above is the detailed content of Tips | A Python library that looks so fake that it cracked me up. .. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:Python当打之年
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template