Boost Your Testing with Python’s Faker: Generate Realistic Data Instantly
Learn how to install and use the Python Faker library to quickly generate realistic fake data—such as names, addresses, emails, and network details—and see a complete example of inserting generated records into a MySQL database for testing purposes.
The Faker library provides a simple way to generate realistic fake data for testing, covering names, addresses, phone numbers, emails, IPs, and many other types.
1. Installation
pip install Faker2. Simple usage
from faker import Faker</code>
<code>fake = Faker(locale='zh_CN')</code>
<code>fake.name()</code>
<code>'李洁'</code>
<code>fake.address()</code>
<code>'上海市兴安盟县江北东莞路r座 803484'The locale parameter selects the cultural locale for generated data; default is en_US. Common locales include zh_CN (Chinese Mainland), zh_TW (Chinese Taiwan), and en_US (English United States).
3. Other methods
3.1 Geographic information
city_suffix()
country()
country_code()
district()
geo_coordinate()
latitude()
longitude()
postcode()
province()
address()
street_address()
street_name()
street_suffix()
3.2 Basic profile
ssn()
bs()
company()
company_prefix()
company_suffix()
credit_card_expire()
credit_card_full()
credit_card_number()
credit_card_provider()
credit_card_security_code()
job()
first_name_female()
first_name_male()
name()
name_female()
name_male()
phone_number()
phonenumber_prefix()
3.3 Email information
ascii_company_email()
ascii_email()
company_email()
email()
safe_email()
3.4 Network basics
domain_name()
domain_word()
ipv4()
ipv6()
mac_address()
tld()
uri()
uri_extension()
uri_page()
uri_path()
url()
user_name()
image_url()
3.5 Browser information
chrome()
firefox()
internet_explorer()
opera()
safari()
linux_platform_token()
user_agent()
3.6 Numeric information
numerify()
random_digit()
random_digit_not_null()
random_int()
random_number()
pyfloat()
pyint()
pydecimal()
3.7 Text and encryption
pystr()
random_element()
random_letter()
paragraph()
paragraphs()
sentence()
sentences()
text()
word()
words()
binary()
boolean()
language_code()
locale()
md5()
null_boolean()
password()
sha1()
sha256()
uuid4()
4. Practical example
import pymysql</code>
<code>from faker import Faker</code>
<code>conn = pymysql.connect(host="114.215.129.166", port=3306, user="nice", password="", db="flask201", charset="utf8")</code>
<code>cursor = conn.cursor()</code>
<code>sql1 = """drop table if exists faker_user"""</code>
<code>sql2 = """
create table faker_user(
pid int primary key auto_increment,
username varchar(20),
password varchar(20),
address varchar(35)
)
"""</code>
<code>cursor.execute(sql1)</code>
<code>cursor.execute(sql2)</code>
<code>fake = Faker("zh_CN")</code>
<code>for i in range(20):</code>
<code> sql = """insert into faker_user(username,password,address) values('%s','%s','%s')""" % (fake.name(), fake.password(special_chars=False), fake.address())</code>
<code> print('姓名:'+fake.name() + '|密码:'+fake.password(special_chars=False) + '|地址:'+fake.address())</code>
<code> cursor.execute(sql)</code>
<code>conn.commit()</code>
<code>cursor.close()</code>
<code>conn.close()Sample output shows generated Chinese names, passwords, and addresses for each inserted record.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
