|
| 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) |