-
Notifications
You must be signed in to change notification settings - Fork 1
/
write_csv.py
45 lines (29 loc) · 1.11 KB
/
write_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
33
34
35
36
37
38
39
40
41
42
43
44
45
import csv
from datetime import datetime
def create_csv_file():
now = datetime.now()
# Building the Date and Time string
current_time = now.strftime("%Y_%m_%d_%H-%M-%S")
# Building the complete filename
csv_filename = "csv/{}_output.csv".format(current_time)
# Open File at specified location
csv_file = open(csv_filename, 'w')
# Create the csv writer
writer = csv.writer(csv_file, dialect='excel')
# Write a row to the file
writer.writerow(['Date', 'Time', 'Direction', 'Total'])
# Return the used filename and path
return csv_filename
def write_new_value(file_name, direction, newtotal):
now = datetime.now()
# Building the Date and Time string
current_time = now.strftime("%H:%M:%S")
current_date = now.strftime("%Y-%m-%d")
# Open File at specified location
csv_file = open(file_name, 'a', newline='')
# Create the csv writer
writer = csv.writer(csv_file, dialect='excel')
# Create list with columns as members
newrow = [current_date, current_time, direction, newtotal]
# Write a row to the file
writer.writerow(newrow)