-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsvf1.py
More file actions
31 lines (25 loc) · 1009 Bytes
/
Copy pathcsvf1.py
File metadata and controls
31 lines (25 loc) · 1009 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import csv
import random
#create new csv file
def create_csv(filename, headers, rows):
with open(filename, mode="w", newline="") as file:
writer = csv.writer(file)
writer.writerow(headers)
writer.writerows(rows)
print(f"CSV file '{filename}' created successfully")
# Function to generate random data
def generate_data(num_rows):
names = ["Alice", "Bob", "Charlie", "David", "Eva", "Frank", "Grace", "Hannah", "Ivy", "Jack"]
departments = ["HR", "Software Developer", "Sales", "Marketing", "Finance", "IT", "Operations", "Legal"]
rows = []
for i in range(1, num_rows + 1):
name = random.choice(names)
age = random.randint(20, 60) # Random age between 20 and 60
department = random.choice(departments)
rows.append([i, name, age, department])
return rows
filename = "sample_data.csv"
headers = ["ID", "Name", "Age", "Department"]
num_rows = 1000
rows = generate_data(num_rows)
create_csv(filename, headers, rows)