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 Python uses Faker to fabricate data

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Python how to use Faker to fake data, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

When testing a program, a lot of data is often needed for testing.

Such as name, sex, age, telephone number, email address and so on.

If you manually create data, such as "Xiaoming", "Xiao Hong", "Zhang San", "Li Si" and so on, have you considered the feelings of these names? they are also very tired.

For example, passwords, the worst passwords on the Internet, are not also "123456", "abcdef" and "asdf".

It is really nerve-wracking and inefficient to produce data in this way, so what should we do?

Faker extension library

At this time, the Python extension library Faker came, with all kinds of data.

Install faker first

Pip install Faker

Create a faker object

From faker import Faker

Fake = Faker ()

Randomly generate a name

Fake.name () # 'Nancy Horton'

The name generated above is in English. What if you want a Chinese name?

Quite simply, when creating a Faker object, you can specify the language "Chinese".

Fake = Faker ("zh_CN")

Some commonly used languages include the following:

Simplified Chinese: zh_CN traditional Chinese: zh_TW American English: en_US British English: en_GB German: de_DE Japanese: ja_JP Korean: ko_KR French: what kind of data do you have in fr_FR

What data can be generated with faker?

Faker provides some Standard Providers and lists some of the more commonly used data.

Addresscompanydate_timejobpersonphone_numberprofilepythonuser_agent

Such as generating "address" information

In [1]: fake.address ()

Out [1]: 'block x, hailing Zhao road, Daye city, Beijing 941837'

What is given here is a whole piece of "address" information, and you can also get individual information through building_number (), city (), street_address (), etc.

It doesn't matter if you can't remember these fields, just consult the official documentation when you need to use them.

Document link

Https://faker.readthedocs.io/en/master/providers.html

Another example is to generate "position" information.

In [2]: fake.job ()

Out [2]: 'electrical / electrical engineer'

Generate the data type of python

# generate a dictionary

In [3]: fake.pydict ()

Out [3]:

{'production': 'OlmMWPfQMJYxeiJtZSFC'

'some': 'jUYzbWgDEqvzjiAsubSX'

'Resources': 7670

'App': 804210265906561.0

'International': 9113

Movie: 'https://yangtang.cn/homepage.jsp',

Aspect: 'RGfbqIgxqTbnjkGDpoVO'

Why: 1947

Address: 7021

'time': 'oxia@hotmail.com'}

Generate user_agent

In [4]: fake.chrome ()

Out [4]: 'Mozilla/5.0 (iPad; CPU iPad OS 4 / 2 / 1 like Mac OS X) AppleWebKit/531.2 (KHTML, like Gecko) CriOS/55.0.807.0 Mobile/62B715 Safari/531.2'

Generate personal information

In [5]: fake.profile ()

Out [5]:

{'job':' Warehouse Manager

'company': 'Taiji Media Co., Ltd.

'ssn': '370302198911123749'

'residence':' 204494', Block k, Shenbei Xinwang Road, Guiying City, Yunnan Province

'current_location': (Decimal (' 32.942144'), Decimal ('95.019663'))

'blood_group': 'Omuri'

'website': ['https://www.minyan.cn/'],'

'username': 'yangwen'

'Jiang Lu 'name':

'sex': 'M'

'address':' Block a, Fengdu Changsha Street, Ningde County, Ningxia Hui Autonomous region 685268'

'mail': 'oliao@yahoo.com'

'birthdate': datetime.date (1971, 2, 13)}

If you want to generate multiple pieces of personal information, you can use the for loop to add each dictionary to the list and export a DataFrame

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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