-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_csv.py
32 lines (24 loc) · 872 Bytes
/
generate_csv.py
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
32
import csv
import random
import string
import uuid
def generate_random_string(string_length=10):
letters = string.ascii_letters
return ''.join(random.choice(letters) for i in range(string_length))
def generate_random_book():
book = {
'uuid': uuid.uuid4(),
'title': generate_random_string(),
'author': '1900-01-01',
'date_published': ''.join(random.choices(string.digits, k=5)),
'publisher': generate_random_string(),
}
return book
def write_books_to_csv(filename):
with open(filename, mode='w', newline='') as file:
fieldnames = ["uuid", "title", "author", "date_published", "publisher"]
writer = csv.DictWriter(file, fieldnames=fieldnames)
writer.writeheader()
for i in range(200):
writer.writerow(generate_random_book())
write_books_to_csv('books1.csv')