-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimportcsv.py
More file actions
80 lines (74 loc) · 2.61 KB
/
importcsv.py
File metadata and controls
80 lines (74 loc) · 2.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import os
import time
import csv
from tkinter import Tk
from tkinter.filedialog import askopenfilename
from database import Database
class ImportCSV:
def __init__(self):
self.options = {
'1': self.choosefile,
}
@staticmethod
def display_menu():
print("""
==========================================
| CSV Import: |
==========================================
| 1: Choose the CSV file |
| q: Main Menu |
==========================================
""")
def run(self):
while True:
os.system('cls' if os.name == 'nt' else 'clear')
self.display_menu()
choice = input("Enter your choice> ")
if choice in self.options:
self.options[choice]()
break
elif choice == 'q':
print("Returning to main menu...")
break
else:
print("Invalid choice, please try again.")
time.sleep(1)
@staticmethod
def importcsv(file):
values = []
with open(file, 'r') as f:
reader = csv.reader(f)
next(reader)
for row in reader:
sid = row[0]
name = row[1]
surname = row[2]
initials = row[3]
age = row[4]
dob = row[5]
values.append("('{}','{}','{}','{}','{}','{}')".format(sid,
name,
surname,
initials,
age,
dob))
db = Database()
db.connect()
db.insert(values)
db.close()
def choosefile(self):
Tk().withdraw()
file = askopenfilename()
if not (len(file) == 0):
if file.endswith('.csv'):
print("Importing {}...".format(file))
tic = time.perf_counter()
self.importcsv(file)
toc = time.perf_counter()
print(f"Successfully imported {file} in {toc - tic:0.4f} seconds")
input("Press any key to return to the main menu...")
else:
print("Invalid file type, please try again.")
time.sleep(1)
else:
self.run()