程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Test the one key construction method of fake data construction, python faker package user manual

編輯:Python

Preface

When we finish the development , We need some fake data to support our system . If you construct it yourself , laborious , It is not possible to construct appropriate data . While using python Of faker package , Constructing fake data is very simple !

Installation tools

pip install faker

establish Faker

After installation , You need to create a Faker object , There are two ways to create , One is to create directly through the constructor , The other is to create .

>>> from faker import Faker, Factory
>>> fake1 = Factory.create() # Create by factory function
>>> fake1.name() # Randomly generate a name
'Austin Parker'
>>> fake2 = Faker() # Create by constructor
>>> fake2.name() # Randomly generate a name
'Linda Castaneda'

You can see , Created faker after , You can call name() Method to randomly generate a name .

Locale settings

The names generated above are all English names , If you want to generate Chinese names , What to do ?

Faker Support setting localization when creating , That is, the designated area .

>>> fake = Faker("zh_CN")
>>> fake.name()
' Xi Jianping '

You can see , After setting localization , Chinese names can be randomly generated .

Generate more types of data

Use Faker In addition to generating names , Many other types of data can also be generated . Here are some common ways to generate type data .

Address

>>> fake.city() # The city name
' Xinji County '
>>> fake.street_name() # Street name
' Jing Street '
>>> fake.country_code() # Country number
'DM'
>>> fake.longitude() # longitude
Decimal('134.520688')
>>> fake.address() # Address
' Qingheyu street, Yidu City, Jilin Province j seat 292426'
>>> fake.province() # Province
' Ningxia Hui Autonomous Region '
>>> fake.latitude() # latitude
Decimal('-14.386640')
>>> fake.street_address() # Street address
' Yilu v seat '
>>> fake.city_suffix() # City
' City '
>>> fake.postcode() # Postal Code
'530435'
>>> fake.country() # Country
' Virgin Islands '
>>> fake.street_suffix() # Street suffix
' Street, '
>>> fake.district() # District
' Anzi '
>>> fake.geo_coordinate(center=None, radius=0.001) # Geographical coordinates
Decimal('52.985293')
>>> fake.city_name() # The city name
' shenyang '
>>> fake.building_number() # Building number
'C seat '

license plate number

>>> fake.license_plate() # license plate number
'26FX4'

Bank

>>> fake.bank_country()
'GB'
>>> fake.iban()
'GB39SNOA2073712937476'
>>> fake.bban()
'NYJX570813729289

Bar code

>>> fake.ean8() # 8 Bit bar code
'63080728'
>>> fake.ean13() # 13 Bit bar code
'0334204949323'
>>> fake.ean(length=8) # User defined digit bar code , Only choose 8 perhaps 13
'81149919'

Color

>>> fake.color_name() # Color name
'SlateGray'
>>> fake.safe_hex_color() # safe Color 16 Hexadecimal number
'#111100'
>>> fake.safe_color_name() # safe Color name
'black'
>>> fake.rgb_color() # Color rgb value
'46,180,218'
>>> fake.hex_color() # Color 16 Hexadecimal number
'#81b632'
>>> fake.rgb_css_color()
'rgb(27,224,190)'

company

>>> fake.catch_phrase()
'Persistent bandwidth-monitored system engine'
>>> fake.company_prefix() # Company name prefix
' Unicom Shike '
>>> fake.company() # Company name
' Founder Technology Information Co., Ltd '
>>> fake.company_suffix() # Company name suffix
' Information Co., Ltd '
>>> fake.bs()
'transition revolutionary action-items'

The credit card

>>> fake.credit_card_full(card_type=None) # Complete card information
'JCB 15 digit\n Xiu-MeiYin paragraph \n180053229428785 07/21\nCVC: 628\n'
>>> fake.credit_card_provider(card_type=None) # Card provider
'VISA 16 digit'
>>> fake.credit_card_expire(start="now", end="+10y", date_format="%m/%y") # Validity of the card
'06/19'
>>> fake.credit_card_number(card_type=None) # Card number
'4946562273912'
>>> fake.credit_card_security_code(card_type=None) # Security password of the card
'985'

currency

>>> fake.cryptocurrency()
('BTC', 'Bitcoin')
>>> fake.cryptocurrency_code()
'USDT'
>>> fake.currency_code()
'SZL'
>>> fake.currency_name()
'Dominican peso'
>>> fake.currency()
('ZWD', 'Zimbabwean dollar')
>>> fake.cryptocurrency_name()
'Cardano'

Time date

>>> fake.date_time(tzinfo=None) # Random date time
datetime.datetime(2001, 3, 18, 17, 57, 44)
>>> fake.iso8601(tzinfo=None) # With iso8601 Date of standard output
'1973-11-16T22:58:37'
>>> fake.date_time_this_month(before_now=True, after_now=False, tzinfo=None) # A date of the month
datetime.datetime(2017, 11, 1, 14, 33, 48)
>>> fake.date_time_this_year(before_now=True, after_now=False, tzinfo=None) # A date of the year
datetime.datetime(2017, 3, 2, 13, 55, 31)
>>> fake.date_time_this_decade(before_now=True, after_now=False, tzinfo=None) # A date in this decade
datetime.datetime(2010, 3, 26, 6, 33, 23)
>>> fake.date_time_this_century(before_now=True, after_now=False, tzinfo=None) # A date of the century
datetime.datetime(2015, 7, 21, 19, 27, 53)
>>> fake.date_time_between(start_date="-30y", end_date="now", tzinfo=None) # A random time between two times
datetime.datetime(2005, 12, 3, 17, 17, 15)
>>> fake.timezone() # The time zone
'America/Guatemala'
>>> fake.time(pattern="%H:%M:%S") # Time ( Customizable format )
'11:21:52'
>>> fake.am_pm() # Random in the afternoon
'PM'
>>> fake.month() # Random months
'02'
>>> fake.month_name() # Random month name
'August'
>>> fake.year() # Random year
'1974'
>>> fake.day_of_week() # Random day of the week
'Sunday'
>>> fake.day_of_month() # One day in a random month
'02'
>>> fake.time_delta() # Random time delay
datetime.timedelta(13371, 27637)
>>> fake.date_object() # Random Date object
datetime.date(1983, 1, 26)
>>> fake.time_object() # Random time object
datetime.time(17, 8, 56)
>>> fake.unix_time() # Random unix Time ( Time stamp )
1223246848
>>> fake.date(pattern="%Y-%m-%d") # Random date ( Customizable format )
'1984-04-20'
>>> fake.date_time_ad(tzinfo=None) # Random date after AD
datetime.datetime(341, 9, 11, 8, 6, 9)

Work

>>> fake.job()
'Development worker, community'

file

>>> fake.unix_partition(prefix=None) # unix Partition
'/dev/sdg5'
>>> fake.file_name(category=None, extension=None) # file name
' notice .flac'
>>> fake.unix_device(prefix=None) # unix equipment
'/dev/vdu'
>>> fake.file_path(depth=1, category=None, extension=None)
'/ cooperation / Country .webm'
>>> fake.file_extension(category=None) # File extension information
'mp3'
>>> fake.mime_type(category=None)
'text/csv'

Internet

>>> fake.ipv4(network=False) # ipv4 Address
'104.225.105.10'
>>> fake.ipv6(network=False) # ipv6 Address
'dea6:ca11:39d0:b49f:fff1:82f1:bf88:698b'
>>> fake.uri_path(deep=None) # uri route
'search/categories'
>>> fake.uri_extension() # uri Extension
'.htm'
>>> fake.uri() # uri
'https://www.wei.com/terms/'
>>> fake.url() # url
'http://zheng.org/'
>>> fake.image_url(width=None, height=None) # picture url
'https://www.lorempixel.com/700/990'
>>> fake.domain_word() # Domain name subject
'hu'
>>> fake.domain_name() # domain name
'hu.cn'
>>> fake.tld() # The domain name suffix
'com'
>>> fake.user_name() # user name
'xia13'
>>> fake.user_agent() # UA
'Opera/8.33.(Windows NT 5.1; an-ES) Presto/2.9.171 Version/10.00'
>>> fake.mac_address() # MAC Address
'd6:38:cc:2a:76:b2'
>>> fake.safe_email() # Secure email
'[email protected]'
>>> fake.free_email() # Free mailbox
'[email protected]'
>>> fake.company_email() # Company email
'[email protected]'
>>> fake.email() # mailbox
'[email protected]'

Phone number

>>> fake.phonenumber_prefix() # Operator segment , The top three mobile phone numbers
132
>>> fake.msisdn()
'5445934248280'
>>> fake.phone_number() # cell-phone number
'18666613199'

Id card number

>>> fake.ssn(min_age=18, max_age=90)
'460201193310128795'
figure

figure

>>> fake.suffix_female()
''
>>> fake.last_name() # surname
' Dong '
>>> fake.suffix_male()
''
>>> fake.first_name_male() # Male name
' Shulan '
>>> fake.name() # full name
' Empty pill '
>>> fake.first_name() # name
' kay '
>>> fake.last_name_male() # Male surname
' but '
>>> fake.name_male() # Female name
' Shen Liu '
>>> fake.romanized_name()
'Xia Xu'
>>> fake.suffix()
''
>>> fake.first_name_female()
' Qin '
>>> fake.last_name_female()
' name of a master archer '
>>> fake.prefix_male()
''
>>> fake.name_female()
' Ming Lu '
>>> fake.prefix_female()
''
>>> fake.first_romanized_name()
'Li'
>>> fake.prefix()
''
>>> fake.last_romanized_name()
'Tan'

profile Character attributes

>>> fake.profile(fields=None, sex=None)
{
'residence': ' Xishanlong street, Hohhot County, Guangdong Province N seat 403716', 'sex': 'M', 'website': ['http://zheng.org/'], 'birthdate': '1971-02-20', 'ssn': '513226197904080189', 'mail': '[email protected]', 'job': 'Ceramics designer', 'current_location': (Decimal('-86.424001'), Decimal('-153.969207')), 'blood_group': '0+', 'address': ' Shenzhen street, Yongchuan, Beijing, Guangdong w seat 974761', 'company': ' Shuangmin electronic media Co., Ltd ', 'username': 'xiajiang', 'name': ' Hanyuhua '}
>>> fake.simple_profile(sex=None)
{
'mail': '[email protected]', 'address': ' Jiangbeiwen street, Nan City, Tibet Autonomous Region v seat 524952', 'username': 'guiying17', 'sex': 'F', 'birthdate': '1995-10-14', 'name': ' Jiang Min '}

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved