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 use faker to falsify data in Python

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to use faker to fake data in Python". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use faker to fake data in Python".

At the beginning of the project development, in order to test conveniently, we always have to create a lot of fake data into the system to simulate the real environment as much as possible.

For example, to create a batch of user names, create a text, or a phone number, or a street address, IP address, and so on.

Usually we basically knock on the keyboard and create a random string, of course, no one knows each other.

Don't do that now.

You can meet all your needs with faker.

Install faker first

Pip install Faker

Create a faker object

From faker import Faker fake = Faker ()

Fake, a name.

> fake.name () 'Joshua Reed'

Fake an address

> fake.address () '554 Hoffman Locks Suite 216\ nElizabethstad, RI 23081'

Fake one browser UA

> > fake.chrome () 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/532.0 (KHTML, like Gecko) Chrome/35.0.870.0 Safari/532.0'

Fake a date object

> fake.date () '1984-08-17' > fake.date_object () datetime.date (1980, 9, 27)

But he can help you fake anything you need. If you don't know what it can fake, you can check it with dir (fake).

It can fake nearly 300 kinds of things. If there is anything you can't meet your needs, you can submit PR like its Github or extend it yourself.

The question is, the above fake things, such as names and streets, are all in English. Does it support Chinese?

I'm sure it can.

As long as you specify the language when creating the Faker object

> fake = Faker ("zh_CN") > fake.name () 'Zhuangyang' > > fake.address () 'Block I, Shenbei Xinjing Street, Taipei County, Zhejiang Province 285123' > fake.phone_number () '13223924289'

You will find that the false data of fake is quite real. In addition to Chinese, it also supports hundreds of languages such as Japanese, Korean, German, etc.

Of course, it also supports command line mode

-h View help documentation

Faker [- h] [--version] [- o output] [- l {bg_BG,cs_CZ,...,zh_CN,zh_TW}] [- r REPEAT] [- s SEP] [- I {package.containing.custom_provider otherpkg.containing.custom_provider}] [fake] [fake argument [fake argument...]] C:\ Users\ lzjun\ workspace\ > faker name Samantha Washington

Here's the problem again.

Can I create my own fake data? For example, I want to randomly generate a User-Agent based on an Android device.

From faker import Faker fake = Faker () from faker.providers import BaseProvider # create a custom provider class MyProvider (BaseProvider): def android_ua (self): return 'xxxxxx' # add a provider fake.add_provider (MyProvider) > > fake.android_ua () >' xxxxxx'

Isn't it too easy. Whether you are a fake news story or a fake wife, you can do it with provider.

In addition, faker is also a library worth studying as source code.

Thank you for your reading, the above is the content of "how to use faker to fake data in Python". After the study of this article, I believe you have a deeper understanding of how to use faker to fake data in Python, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Development

Wechat

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

12
Report