Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to generate Python Module of Virtual data by faker Generator

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how the faker generator generates the Python module of virtual data. I hope you will get something after reading this article. Let's discuss it together.

Preface

Faker module, a Python-based test data generation tool, whether it is used to initialize the database, create XML files, or generate stress test data, Faker is a good choice.

1.Faker toolkit

Just use pip to download the toolkit

Pip install faker

If the download speed is slow, you can use domestic mirror sources to download.

Domestic Source:-Tsinghua: https://pypi.tuna.tsinghua.edu.cn/simple- Ali Yun: http://mirrors.aliyun.com/pypi/simple/- University of Science and Technology of China https://pypi.mirrors.ustc.edu.cn/simple/- Huazhong University of Science and Technology: http://pypi.hustunique.com/- Shandong University of Technology: http://pypi.sdutlinux.org/- Douban: http://pypi.douban.com / simple/ for example: pip3 install-I https://pypi.doubanio.com/simple/ faker2. Generate data 2.1 to get random data

Use faker.Factory.create () to create and initialize the faker generator, which can generate data by accessing properties named according to the desired data type

From faker import Fakerfaker = Faker () # create object print ('name:', faker.name ()) print (' address:', faker.address ()) print ('text:', faker.text ())

The running results are as follows:

Name: Jeremiah Brownaddress: 32553 Brian Village Suite 438West Jared, SC 86824text: Care professional decision beautiful office although indeed. Newspaper away can prove history hour bank table. Area wear effect action war.2.2 generates localized data

When creating an object, you can specify the region and language to generate the desired test data. Of course, you can also support Chinese data. Just add "zh_CN" data to the created object.

From faker import Fakerfaker = Faker ("zh_CN") for i in range (10): print ('name:', faker.name ())

The running result is as follows

Name: Zhang Tao name: Wang Jinfeng name: Cheng Xiufang name: Liu Feng name: Chen Yang name: Liu Hui name: Yu Liu name: song Yu name: Zhang Yan name: Dai Bo

This toolkit provides several methods to generate data, and it is very simple to use these methods to generate detailed data of a "girlfriend".

Faker.providers.address # address

Faker.providers.company # Inc.

Faker.providers.internet # Network Information

Faker.providers.job # work

Faker.providers.lorem # text

Faker.providers.person # individuals

Faker.providers.phone_number # Mobile phone number

Ker.providers.ssn

The code is as follows:

From faker import Fakerfaker = Faker ("zh_CN") print ('name:', faker.name ()) # Random output Chinese name print ('contact details:', faker.phone_number ()) # Random output phone number print ('address:', faker.address ()) # Random output address print ('Company:', faker.bs ()) # Random output Company print ('Job:' Faker.job () # Random output work print ('mailbox:', faker.company_email ()) # Random output mailbox print ('text:', faker.text ()) finished reading this article I believe you have a certain understanding of "faker generator how to generate virtual data Python module". If you want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report