Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0e625fa

Browse files
committed
add fake user data generator tutorial
1 parent14ffbf2 commit0e625fa

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
5454
-[How to Implement the Affine Cipher in Python](https://thepythoncode.com/article/how-to-implement-affine-cipher-in-python). ([code](ethical-hacking/implement-affine-cipher))
5555
-[How to Crack the Affine Cipher in Python](https://thepythoncode.com/article/how-to-crack-the-affine-cipher-in-python). ([code](ethical-hacking/crack-affine-cipher))
5656
-[How to Implement the Vigenère Cipher in Python](https://thepythoncode.com/article/implementing-the-vigenere-cipher-in-python). ([code](ethical-hacking/implement-vigenere-cipher))
57+
-[How to Generate Fake User Data in Python](https://thepythoncode.com/article/generate-fake-user-data-in-python). ([code](ethical-hacking/fake-user-data-generator))
5758

5859
-###[Machine Learning](https://www.thepythoncode.com/topic/machine-learning)
5960
-###[Natural Language Processing](https://www.thepythoncode.com/topic/nlp)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#[How to Generate Fake User Data in Python](https://thepythoncode.com/article/generate-fake-user-data-in-python)
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# Import necessary libraries and modules.
2+
fromfakerimportFaker
3+
fromfaker.providersimportinternet
4+
importcsv
5+
6+
7+
# Function to generate user data with the specified number of users.
8+
defgenerate_user_data(num_of_users):
9+
# Create a Faker instance.
10+
fake=Faker()
11+
# Add the Internet provider to generate email addresses and IP addresses.
12+
fake.add_provider(internet)
13+
14+
# Initialize an empty list to store user data.
15+
user_data= []
16+
# Loop to generate data for the specified number of users.
17+
for_inrange(num_of_users):
18+
# Create a dictionary representing a user with various attributes.
19+
user= {
20+
'Name':fake.name(),
21+
'Email':fake.free_email(),
22+
'Phone Number':fake.phone_number(),
23+
'Birthdate':fake.date_of_birth(),
24+
'Address':fake.address(),
25+
'City':fake.city(),
26+
'Country':fake.country(),
27+
'ZIP Code':fake.zipcode(),
28+
'Job Title':fake.job(),
29+
'Company':fake.company(),
30+
'IP Address':fake.ipv4_private(),
31+
'Credit Card Number':fake.credit_card_number(),
32+
'Username':fake.user_name(),
33+
'Website':fake.url(),
34+
'SSN':fake.ssn()
35+
}
36+
# Append the user data dictionary to the user_data list.
37+
user_data.append(user)
38+
39+
# Return the list of generated user data.
40+
returnuser_data
41+
42+
43+
# Function to save user data to a CSV file.
44+
defsave_to_csv(data,filename):
45+
# Get the keys (column names) from the first dictionary in the data list.
46+
keys=data[0].keys()
47+
# Open the CSV file for writing.
48+
withopen(filename,'w',newline='')asoutput_file:
49+
# Create a CSV writer with the specified column names.
50+
writer=csv.DictWriter(output_file,fieldnames=keys)
51+
# Write the header row to the CSV file.
52+
writer.writeheader()
53+
# Iterate through each user dictionary and write a row to the CSV file.
54+
foruserindata:
55+
writer.writerow(user)
56+
# Print a success message indicating that the data has been saved to the file.
57+
print(f'[+] Data saved to{filename} successfully.')
58+
59+
60+
# Function to save user data to a text file.
61+
defsave_to_text(data,filename):
62+
# Open the text file for writing.
63+
withopen(filename,'w')asoutput_file:
64+
# Iterate through each user dictionary.
65+
foruserindata:
66+
# Iterate through key-value pairs in the user dictionary and write to the text file.
67+
forkey,valueinuser.items():
68+
output_file.write(f"{key}:{value}\n")
69+
# Add a newline between users in the text file.
70+
output_file.write('\n')
71+
# Print a success message indicating that the data has been saved to the file.
72+
print(f'[+] Data saved to{filename} successfully.')
73+
74+
75+
# Function to print user data vertically.
76+
defprint_data_vertically(data):
77+
# Iterate through each user dictionary in the data list.
78+
foruserindata:
79+
# Iterate through key-value pairs in the user dictionary and print vertically.
80+
forkey,valueinuser.items():
81+
print(f"{key}:{value}")
82+
# Add a newline between users.
83+
print()
84+
85+
86+
# Get the number of users from user input.
87+
number_of_users=int(input("[!] Enter the number of users to generate: "))
88+
# Generate user data using the specified number of users.
89+
user_data=generate_user_data(number_of_users)
90+
91+
# Ask the user if they want to save the data to a file.
92+
save_option=input("[?] Do you want to save the data to a file? (yes/no): ").lower()
93+
94+
# If the user chooses to save the data.
95+
ifsave_option=='yes':
96+
# Ask the user for the file type (CSV, TXT, or both).
97+
file_type=input("[!] Enter file type (csv/txt/both): ").lower()
98+
99+
# Save to CSV if the user chose CSV or both.
100+
iffile_type=='csv'orfile_type=='both':
101+
# Ask the user for the CSV filename.
102+
custom_filename_csv=input("[!] Enter the CSV filename (without extension): ")
103+
# Concatenate the filename with the .csv extension.
104+
filename_csv=f"{custom_filename_csv}.csv"
105+
# Call the save_to_csv function to save the data to the CSV file.
106+
save_to_csv(user_data,filename_csv)
107+
108+
# Save to TXT if the user chose TXT or both.
109+
iffile_type=='txt'orfile_type=='both':
110+
# Ask the user for the TXT filename.
111+
custom_filename_txt=input("[!] Enter the TXT filename (without extension): ")
112+
# Concatenate the filename with the .txt extension.
113+
filename_txt=f"{custom_filename_txt}.txt"
114+
# Call the save_to_text function to save the data to the text file.
115+
save_to_text(user_data,filename_txt)
116+
117+
# If the user entered an invalid file type.
118+
iffile_typenotin ['csv','txt','both']:
119+
# Print an error message indicating that the file type is invalid.
120+
print("[-] Invalid file type. Data not saved.")
121+
# If the user chose not to save the data, print it vertically.
122+
else:
123+
# Call the print_data_vertically function to print the data vertically.
124+
print_data_vertically(user_data)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Faker

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp