-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv_utils.py
57 lines (48 loc) · 1.83 KB
/
csv_utils.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""
Dummy csv generator
"""
import csv
from data_utils import clean_rows, get_n_random_combinations
from generators import email_generator, add_new_column
def parse_csv(name, delimiter=',', qchar='"', newline='', encoding='utf-8'):
"""
Paser csv file and return header and rows
"""
body = []
header = None
with open(name, newline=newline, encoding=encoding) as csvfile:
reader = csv.reader(csvfile, delimiter=delimiter, quotechar=qchar)
for row in reader:
if not header:
header = tuple(row)
else:
body.append(tuple(row))
return header, body
def get_combinations(filename, delimiter, newline, quantity, qchar, encoding):
"""
Return combinations quantity for a file
"""
_, rows = parse_csv(filename, delimiter, qchar, newline, encoding)
combinations = get_n_random_combinations(rows, quantity)
combinations = clean_rows(combinations, rows)
return len(combinations)
def generate_csv(filename, delimiter, newline, quantity, qchar, encoding):
"""
Return rows with dummy data generated
"""
header, rows = parse_csv(filename, delimiter, qchar, newline, encoding)
combinations = get_n_random_combinations(rows, quantity)
combinations = clean_rows(combinations, rows)
header, rows = add_new_column(
header, combinations, 'email', email_generator)
return header, rows
def write_csv(outputfile, delimiter, newline, qchar, encoding, header, rows):
"""
Write csv rows in a file
"""
with open(outputfile, 'w', newline=newline, encoding=encoding) as csvfile:
writer = csv.writer(csvfile, delimiter=delimiter,
quotechar=qchar, quoting=csv.QUOTE_MINIMAL)
writer.writerow(header)
for row in rows:
writer.writerow(row)