This repository was archived by the owner on Mar 16, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv_to_db.py
More file actions
51 lines (45 loc) · 1.61 KB
/
Copy pathcsv_to_db.py
File metadata and controls
51 lines (45 loc) · 1.61 KB
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
# Copyright 2018 Alexander Gompper
# Released under MIT License
import csv
import json
import random
import string
def main():
print('<<< WHATBOT CSV PROFILE READER - CSV TO DB >>>')
try:
with open('input/csv/billing.csv') as input_file:
input_reader = csv.DictReader(input_file, fieldnames=[
'name',
'firstName',
'lastName',
'phone',
'email',
'address',
'address2',
'city',
'state',
'zipCode',
'cardName',
'cardNumber',
'cardMonth',
'cardYear',
'cardCvv',
'_id',
'quickTask'
])
next(input_reader)
try:
with open('output/db/billing.db', 'wb') as output_file:
for row in input_reader:
row['_id'] = ''.join(random.choices(string.ascii_letters + string.digits, k=16)) if row['_id'] in {'', None} else row['_id']
row['quickTask'] = True if row['quickTask'].lower() == 'true' else False
output_file.write(bytes((json.dumps(row) + '\n').encode('utf-8')))
except IOError:
print('<<< [error] something went wrong writing the billing.db output file >>>')
exit(-1)
print('<<< COMPLETE >>>')
except IOError:
print('<<< [error] something went wrong opening the billing.csv input file >>>')
exit(-1)
if __name__ == '__main__':
main()