Python? Faker creates fake data

Keywords: Unix Python network pip

Reprint: https://www.cnblogs.com/hellangels333/p/9039784.html

faker creates fake data

In our work, sometimes we need to forge some fake data. How to use Python to forge these seemingly true fake data?
Python has a package called Faker, which can be used to easily forge names, addresses, phone numbers and other information.

1. Install faker package

pip install faker

2. After installation, you need to create a Faker object first. There are two ways to create it. One is directly through the constructor, and the other is through the factory function.

>>> 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'

3. Various data items

3.1 Chinese name

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

3.2 address

>>> fake.city() # City name
'Xinji County'
>>> fake.street_name() # Street name
'Jingjie'
>>> fake.country_code() # Country code
'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
'Yi Lu 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() # area
'Anci'
>>> fake.geo_coordinate(center=None, radius=0.001) # Geographic coordinates
Decimal('52.985293')
>>> fake.city_name() # City name
'Shenyang'
>>> fake.building_number() # Building number
'C Seat'

3.3 license plate number

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

4. bar code

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

5. colors

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

6. companies

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

7. credit card

>>> fake.credit_card_full(card_type=None) # Full card information
'JCB 15 digit\n Xiu Mei Duan\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 card
'06/19'
>>> fake.credit_card_number(card_type=None) # Card number
'4946562273912'
>>> fake.credit_card_security_code(card_type=None) # Security code of the card
'985'

8. 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'

9. Time and date

>>> fake.date_time(tzinfo=None) # Random date time
datetime.datetime(2001, 3, 18, 17, 57, 44)
>>> fake.iso8601(tzinfo=None) # Date of ISO 8601 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) # 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 the year
datetime.datetime(2010, 3, 26, 6, 33, 23)
>>> fake.date_time_this_century(before_now=True, after_now=False, tzinfo=None)  # A date of this 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() # time zone
'America/Guatemala'
>>> fake.time(pattern="%H:%M:%S") # Time (customizable format)
'11:21:52'
>>> fake.am_pm() # Random am PM
'PM'
>>> fake.month() # Random month
'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() # Random day of the 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 (timestamp)
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)

10. work

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

11. document

>>> 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 device
'/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'

12. 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 path
'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() # Domain 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() # Security mailbox
'mingli@example.net'
>>> fake.free_email() # Free mailbox
'tao44@gmail.com'
>>> fake.company_email()  # Company mailbox
'jingzhong@wang.cn'
>>> fake.email() # mailbox
'changjun@hao.com'

13. Telephone number

>>> fake.phonenumber_prefix() # Top three mobile phone numbers in operator name segment
132
>>> fake.msisdn()
'5445934248280'
>>> fake.phone_number() # Cell-phone number
'18666613199'

14. ID card number

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

15. characters

>>> fake.suffix_female()
''
>>> fake.last_name() # surname
'Dong'
>>> fake.suffix_male()
''
>>> fake.first_name_male() # Male name
'Shu Lan'
>>> fake.name() # Full name
'Kong Dan'
>>> fake.first_name() # name
'Kai'
>>> fake.last_name_male() # Male surname
'but'
>>> fake.name_male() # Female names
'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'

16.profile Character properties

>>> fake.profile(fields=None, sex=None)
{'residence': 'Xishanlong street, Hohhot County, Guangdong Province N Seat 403716', 'sex': 'M', 'website': ['http://,'username ':'xiajiang','na Me ':' Han Yuhua '}
>>> fake.simple_profile(sex=None)
{'mail': 'dengna@yahoo.com', 'address': 'Jiangbeiwen street, Nanshi, Tibet Autonomous Region v Seat 524952', 'username': 'guiying17', 'sex': 'F', 'birthdate': '1995-10-14', 'name': 'Jiang Min'}

17.lorem

>>> fake.paragraphs(nb=3, ext_word_list=None)
['Of course, the analysis of the choice gives us a sense of.', 'Apply this ability between locations.', 'Your handling of Shanghai.People download mainly from the homepage.The picture has all the details.']
>>> fake.word(ext_word_list=None)
'Maximum'
>>> fake.text(max_nb_chars=200, ext_word_list=None)
'Related pictures completed and people your.When the language plan appears, please pay attention to the handling.\n Just like making personal message, this is the explanation.The main reason for this is that there is a.Company time series recommendation date.\n Car school discovery method.Co student she looks at all times or.\n Shenzhen world culture is not the result.Some of them have goods to comment on the market finally.Data reply name thank you series.\n Government prestige two of those one of them.Those of the following need to.\n They don't work with resources, all those articles.What's not the current system.'
>>> fake.sentences(nb=3, ext_word_list=None)
['Education and release in the way of information.', 'Business type social work method.', 'The author of the post is.']
>>> fake.paragraph(nb_sentences=3, variable_nb_sentences=True, ext_word_list=None)
'Can't add the most important.Find out pay attention to free production this is.'
>>> fake.words(nb=3, ext_word_list=None)
['about', 'Realization', 'home page']
>>> fake.sentence(nb_words=6, variable_nb_words=True, ext_word_list=None)
'Find a little success system space national comparison.'

Posted by james182 on Sat, 30 Nov 2019 10:09:12 -0800