-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcsv_reader.py
33 lines (26 loc) · 1.05 KB
/
csv_reader.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
import csv
from time import time
start_time = time()
with open('employee_file.csv', newline='') as csvfile:
rdr = csv.reader(csvfile)
end_time = time()
seconds_elapsed = end_time - start_time
hours, rest = divmod(seconds_elapsed, 3600)
minutes, seconds = divmod(rest, 60)
print(f'It took {hours} hours, {minutes} minutes and {seconds} seconds to read in the file.')
start_time = time()
l = sorted(rdr, key=lambda x: x[0], reverse=False)
end_time = time()
seconds_elapsed = end_time - start_time
hours, rest = divmod(seconds_elapsed, 3600)
minutes, seconds = divmod(rest, 60)
print(f'It took {hours} hours, {minutes} minutes and {seconds} seconds to sort the file.')
start_time = time()
with open('employee_file_sorted.csv', 'w') as csvout:
wrtr = csv.writer(csvout)
wrtr.writerows(l)
end_time = time()
seconds_elapsed = end_time - start_time
hours, rest = divmod(seconds_elapsed, 3600)
minutes, seconds = divmod(rest, 60)
print(f'It took {hours} hours, {minutes} minutes and {seconds} seconds to write the file.')