Refer to the website
https://zhuanlan.zhihu.com/p/87203290
Copy the code
preface
In the process of writing programs, we often need to use a lot of data to 'test'. If you 'manually create data', you must spend a lot of energy, which is not reasonable. At this point we should use Faker, the Python library (extension package), to generate all kinds of 'pseudo-data'.Copy the code
The installation
pip install faker
Copy the code
Basic usage
from faker import Faker Import faker from the faker module.
fake = Faker() Save to the variable fake.
fake.name() Call the name() method to randomly generate a name.
# Donna Kelly
fake.address() Call the address() method to randomly generate the address information.
# 519 Donna River
If we want to generate random data in Chinese, we can pass 'zh_CN' to the locale parameter on instantiation:
from faker import Faker
fake = Faker(locale='zh_CN')
fake.name()
# PangChao
fake.address()
489476, Block P, Hechuan Zhang Street, Xinji County, Hebei Province
# if you want to generate traditional Chinese characters, you can pass in the value 'zh_TW' for Taiwan, China:
from faker import Faker
fake = Faker(locale='zh_TW')
fake.name()
# Luo Wanting
fake.address()
0, No. 35, Shuiyuan Lane, Dali County
If we want to generate data for another language or locale, we can pass in the corresponding locale: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 the code
Faker’s other methods
Note: Individual methods are targeted. For example, the province() method is applicable to China, but not to the United States and some other countries.Copy the code
Address associated
fake.address() # address
# 'Block K, Shangzhong Street, Daye County, HKSAR 664713'
fake.building_number() # buildings of
A '#' v
fake.city() # Full city name
# 'Changchun County'
fake.city_name() # City name (without city/county)
# 'Wuzhou'
fake.city_suffix() # city suffix
# 'city'
fake.country() # Country name
# 'Eritrea'
fake.country_code(representation="alpha-2")
# 'BZ' # country number
fake.district() # region
# 'Sand Bay'
fake.postcode() # zip code
# '332991'
fake.province() # province
# 'Hebei'
fake.street_address() # Street address
Block D, Wuhan Street
fake.street_name() # Street name
# 'Guangzhou Road'
fake.street_suffix() # Street suffix
# 'road'
Copy the code
Automotive related
fake.license_plate() # licence
# 'ZCO 000'
Copy the code
Bank related
fake.bank_country() # Country of the bank
# 'GB'
fake.bban() # Basic bank account
# 'TPET9323218579379'
fake.iban() # International banking code
# 'GB82IRVM1531009974701'
Copy the code
Bar code correlation
fake.ean(length=13) # EAN bar code
# '5456457843465'
fake.ean13() # EAN13 barcode
# '2689789887590'
fake.ean8() # EAN8 barcode
# '52227936'
Copy the code
Color related
fake.color_name() # color name
# 'Orange'
fake.hex_color() # Color hexadecimal value
# '#a5cb7c'
fake.rgb_color() # color RGB value
# '15,245,42'
fake.rgb_css_color() # CSS color values
# 'RGB (15,70,13)'
fake.safe_color_name() # safety color
# 'aqua'
fake.safe_hex_color() # Secure hexadecimal value
# # '881100'
Copy the code
The company related to
fake.bs() # Business words
# 'synthesize strategic vortals'
fake.catch_phrase() # Catchphrase (slogan)
# 'Robust even-keeled service-desk'
fake.company() # Company name
# 'Fusi Technologies LTD.'
fake.company_prefix() # Company name prefix
# 'Shangsof.com'
fake.company_suffix() # Company name suffix
# 'Network Limited'
Copy the code