Batch generation of false data by python with faker library

  • 2021-09-24 22:58:25
  • OfStack

Catalog wedge
How to use faker
Basic use
Geographic information basic information computer foundation, Internet information network basic information browser information digital class
Text, Encryption Class Time Information Class
Summary

Wedge

When we do tests at ordinary times, we often use some fake data, and there is a package in Python called faker (not the one that plays LOL), which is specially used to generate fake data, and the generated fake data is very realistic. Let's take a look at it below.

How to use faker

Basic use

faker is very simple to use, we can see it by looking at it once.


from faker import Faker

#  Import Faker This class ,  Just instantiate 
fake = Faker(locale="zh_CN")

#  Then call the method inside to generate the corresponding fake data 
print(fake.name()) #  Xie Xiuhua 
print(fake.address()) # 4 Baiyun Shenyang Road, Lixian County, Sichuan Province o Seat  313522

It looks quite realistic, but there is one point to note. The address here is not a real address, but a random combination, that is, a random combination of provinces, cities and roads.

In addition, when we instantiate the class Faker, we specify locale= "zh_CN", which means to generate Chinese data, because the default is English data. faker can generate fake data in multiple languages, and the supported languages are as follows:

Simplified Chinese: zh_CN Traditional Chinese: zh_TW US English: en_US British English: en_GB German: de_DE Japanese: ja_JP Korean: ko_KR French: fr_FR

Let's take a chestnut:


from faker import Faker

fake = Faker(locale="ja_JP")

#  Designated as Japan 
print(fake.name()) #  Zhu   COMPARISON COMPARISON 
print(fake.address()) #  Huachuan, Odomachi, Muhidomo County 41 Ding Mu 7 Fan 10 No.   The middle shrine of the Countari 994

#  Designated as Taiwan 
fake = Faker(locale="zh_TW")
print(fake.name()) #  Maggie 
print(fake.address()) # 23149  New Fuzhou Lane 3 Segment 653 In fact, in fact, the 3 In fact, in fact, the 

In addition to the above name and address, faker also supports generating many other data.

Geographic information category


from faker import Faker

fake = Faker(locale="zh_CN")

# 1.  Random printing 1 Countries ,  At this time follow Faker Inside locale Irrelevant 
print(fake.country()) #  Djibouti 

# 2.  Country code ,  At this time follow Faker Inside locale Irrelevant 
print(fake.country_code()) # KW

# 3.  Printing area ,  At this time, it is China's district 
print(fake.district()) #  Sunrise 

# 4.  Print latitude 
print(fake.latitude()) # -39.076213

# 5.  Print longitude 
print(fake.longitude()) # 138.988316

# 6.  Print Zip Code 
print(fake.postcode()) # 881534

# 7.  Print province 
print(fake.province()) #  Xinjiang Uygur Autonomous Region 

# 8.  Print detailed address 
print(fake.address()) #  Yongan Street, Fengdu, Shuying County, Hebei Province D Seat  260864

# 9.  Print street address 
print(fake.street_address()) #  Ginger Street c Seat 

# 10.  Print street name 
print(fake.street_name()) #  Haimen Street 

# 11.  Get city and county  ( What you print is a suffix ,  It's either a county or a city )
print(fake.city_suffix()) #  City 
print(fake.city_suffix()) #  County 

# 12.  Print street, road ( What you print is a suffix ,  It's either a road or a street )
print(fake.street_suffix()) #  Road 
print(fake.street_suffix()) #  Street 

Basic information class


from faker import Faker

fake = Faker(locale="zh_CN")

# 1.  Generate ID number 
print(fake.ssn()) # 513428197105280989

# 2.  Generate company service name 
print(fake.country_code()) # HT

# 3.  Random company name (long) 
print(fake.company()) #  Chuanglian Century Technology Co., Ltd 

# 4.  Random company name (short) 
print(fake.company_prefix()) #  Xincheng Zhiyuan 

# 5.  Nature of the Company 
print(fake.company_suffix()) #  Information Co., Ltd 

# 6.  Random credit card maturity date 
print(fake.credit_card_expire()) # 03/28

# 7.  Generate complete credit card information 
print(fake.credit_card_full())
"""
xx JCB 16 digit
 Yong   Zhao 
3505427828999283 12/22
CVC: 463

"""

# 8.  Credit card number 
print(fake.credit_card_number()) # 44980244740048

# 9.  Credit card type 
print(fake.credit_card_provider()) # Mastercard

# 10.  Credit card security code 
print(fake.credit_card_security_code()) # 581

# 11.  Random position 
print(fake.job()) #  New media operation 

# 12.  The first name of a woman 
print(fake.first_name_female()) #  Lijuan 

# 13.  Male first name 
print(fake.first_name_male()) #  Bin 

# 14.  Female surname and male surname ,  The last name here is combined with the first name above to get 1 Full name 
#  To tell the truth, I personally think last_name_female It's a bit awkward to print your last name ,  Maybe foreigners are used to writing their last names after and their first names before 
#  In addition, regarding surnames, I personally feel that there is not much difference between men and women 
print(fake.last_name_female()) #  Gao 
print(fake.last_name_male()) #  Yang 

# 15.  Print full name 
print(fake.name()) #  Xiaohong Tian 

# 16.  Full male name 
print(fake.name_male()) #  Zhu Yan 

# 17.  Full name of woman 
print(fake.name_female()) #  Wang Kai 

# 18.  Randomly generate mobile phone numbers 
print(fake.phone_number()) # 18035736881

# 19.  Randomly generate mobile phone number segments 
print(fake.phonenumber_prefix()) # 147

Computer Fundamentals, Internet Information Class


from faker import Faker

fake = Faker(locale="zh_CN")

# 1.  Random ASCII Company mailbox name 
print(fake.ascii_company_email()) # minghan@jy.org

# 2.  Random ASCII Mailbox 
print(fake.ascii_email()) # gang38@yongcui.cn

# 3.  Company mailbox 
print(fake.company_email()) # xiuyinggao@guoxia.org

# 4.  Mailbox 
print(fake.email()) # yang28@hotmail.com

# 5.  Secure mailbox 
print(fake.safe_email()) # jieqiao@example.com

Network basic information class


from faker import Faker

fake = Faker(locale="zh_CN")

# 1.  Generate domain name 
print(fake.domain_name()) # lijuan.net

# 2.  Domain word ( That is, no suffix is included )
print(fake.domain_word()) # weiyang

# 3.  Random IP4 Address 
print(fake.ipv4()) # 12.16.224.95

# 4.  Random IP6 Address 
print(fake.ipv6()) # e6b:5814:4d63:1496:9e0d:eb44:e114:722b

# 5.  Random MAC Address 
print(fake.mac_address()) # 8a:1c:ce:eb:f0:5c

# 6.  Website domain suffix (com, net, cn Wait, not including .)
print(fake.tld()) # cn

# 7.  Random URI Address 
print(fake.uri()) # https://na.cn/login/

# 8.  URL file suffix 
print(fake.uri_extension()) # .htm

# 9.  URL file (without suffix) 
print(fake.uri_page()) # homepage

# 10.  URL file path (excluding file name) 
print(fake.uri_path()) # categories

# 11.  Random URL Address 
print(fake.url()) # https://jingsong.cn/

# 12.  Random user name 
print(fake.user_name()) # jing13

# 13.  Random URL Address 
print(fake.image_url()) # https://dummyimage.com/109x622

Browser information class


from faker import Faker

fake = Faker(locale="zh_CN")

# 1.  Random generation Chrome Browser of user_agent Information 
print(fake.chrome()) 
# Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_7) AppleWebKit/535.0 (KHTML, like Gecko) Chrome/26.0.870.0 Safari/535.0

# 2.  Random generation FireFox Browser of user_agent Information 
print(fake.firefox()) 
# Mozilla/5.0 (Android 3.2.2; Mobile; rv:15.0) Gecko/15.0 Firefox/15.0

# 3.  Random generation IE Browser of user_agent Information 
print(fake.internet_explorer())
# Mozilla/5.0 (compatible; MSIE 8.0; Windows 98; Win 9x 4.90; Trident/3.1)

# 4.  Random generation Opera Browser of user_agent Information 
print(fake.opera()) 
# Opera/9.47.(X11; Linux i686; cv-RU) Presto/2.9.186 Version/11.00

# 5.  Random generation Safari Browser of user_agent Information 
print(fake.safari()) 
"""
Mozilla/5.0 (iPod; U; CPU iPhone OS 4_2 like Mac OS X; ne-NP) 
AppleWebKit/534.29.1 (KHTML, like Gecko) Version/3.0.5 Mobile/8B119 Safari/6534.29.1
"""

# 6.  Random Linux Information 
print(fake.linux_platform_token()) # X11; Linux x86_64

# 7.  Random user_agent Information 
print(fake.user_agent()) 
# Mozilla/5.0 (Windows NT 5.0; si-LK; rv:1.9.1.20) Gecko/2017-09-23 19:50:28 Firefox/3.8

Digital class

This is of little significance. It can be realized by using the built-in module of Python or numpy, which is not mentioned here.

Text, cryptographic class


 
from faker import Faker

fake = Faker(locale="zh_CN")

# 1. 随机字符串
print(fake.pystr()) # nyfOoBhwiCAYECiKOULE

# 2. 随机字母
print(fake.random_element()) # c

# 3. 随机字母
print(fake.random_letter()) # A

# 4. 随机生成1个段落
print(fake.paragraph()) # 产品特别为了文章.研究不同评论就是还是工程.

# 5. 随机生成多个段落
print(fake.paragraphs())
"""
['1下手机希望责任.合作空间联系最大.', 
'用户免费中心部分就是如何男人.次数美国自己影响程序服务.你的类别欢迎日本是1.', 
'数据情况工作1起发展应该自己.发展资源内容.']
"""

# 6. 随机生成1句话
print(fake.sentence()) # 更新法律学生应用.

# 7. 随机生成多句话,与段落类似
print(fake.sentences()) # ['服务不过而且对于美国今天价格.', '发布全国语言帖子.', '产品关系问题产品.']

# 8. 随机生成1篇文章
print(fake.text())
"""
这是价格方法阅读.发布对于经济地区开发相关你们合作.
1些更新个人学生重要.什么威望网络影响.
市场这些环境阅读那个类别.软件男人怎么等级状态日本您的.
您的阅读地方论坛.广告帮助位置企业.以下应用运行主要当前只是.
简介只要来自如果科技.那么文化进入公司电话.
名称威望游戏报告最新日期论坛.最大为了如何提高大学.还是文件因为喜欢语言.的是以及资料是否现在两个.
"""

# 9. 随机生成词语
print(fake.word()) # 过程

# 10. 随机生成多个词语,用法与段落,句子,类似
print(fake.words()) # ['任何', '参加', '正在']

# 11. 随机生成2进制编码
print(fake.binary()) # 非常长, 不打印了

# 12. 随机生成两位语言编码
print(fake.language_code()) # ks

# 13. 随机生成语言/国际 信息
print(fake.locale()) # shs_CA

# 14. 随机生成MD5
print(fake.md5()) # 0df0396328281f32b1922cd82016580d

# 15. 随机生成密码,可选参数:
# length:密码长度;special_chars:是否能使用特殊字符;digits:是否包含数字;upper_case:是否包含大写字母;lower_case:是否包含小写字母
print(fake.password()) # C^!5fIk!s8

# 16. 随机SHA1
print(fake.sha1()) # e7d9fa4f3e9a25e5ab44d88971e4c56ef4cc446f

# 17. 随机SHA256
print(fake.sha256()) # 47f9163f2a72edc855ef25289c2cfcd4c213842607b4b936c12a43b477c8ed77

# 18. 随机UUID
print(fake.uuid4()) # 45dc0754-b3e3-4c94-bdcb-d20a3a8f4d9d

Time information class

It is of little significance. If you are interested, you can understand it yourself.

Summary

Personally, faker is the most useful library to generate names and addresses. In fact, although it can generate a lot of information, there are only a few commonly used ones. When you need to use it, you can check it directly.

The above is python with faker library batch generation of false data details, more about python faker library generation of false data, please pay attention to other related articles on this site!


Related articles: