-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcsv_conversion.py
66 lines (54 loc) · 1.86 KB
/
csv_conversion.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
58
59
60
61
62
63
64
65
66
# -*- coding: utf-8 -*-
import time
from settings import CSV_INPUT_FILE, CSV_OUTPUT_FILE, CSV_CLEAR_INFILE, REC_FILE
def reformat_date(datefield):
"""Convert pretty date to date separated by columns."""
date = time.strptime(datefield, "%a %b %d %w %Y at %H:%M:%S")
out = time.strftime("%Y,%m,%d,%w,%H,%M,%S,", date)
timeidx = (date.tm_sec + date.tm_min*60 + date.tm_hour*3600)/86400 + \
date.tm_wday
out += str(timeidx) + ',-0500'
return out
readfilename = REC_FILE
if CSV_INPUT_FILE:
readfilename = CSV_INPUT_FILE
with open(readfilename, 'r') as record:
lines = record.readlines()[4:]
newlines = []
i = 0
for line in lines:
# first, split on the commas and strip whitespace
line = line.split(', ')
line = [field.strip() for field in line]
fields = []
i += 1
fields.append(reformat_date(line[0]))
# check the length of `line`. if 3, connection error; if 5, normal
if len(line) == 3:
fields.append(line[1])
# connection error
if "downloaded" in line[1]:
# download error
fields.append("Download failure")
elif "uploaded" in line[1]:
# upload error
fields.append("Upload failure")
else:
# total error
fields.append("Connection failure")
elif len(line) == 5:
# normal
fields.extend(line[1:])
else:
print(line)
print("Critical: The above line is misformatted. No data has been")
print(" written to {} or cleared from {}.".format(
CSV_OUTPUT_FILE, readfilename))
assert False, "Misformatted line"
line = ','.join(fields) + '\n'
newlines.append(line)
with open(CSV_OUTPUT_FILE, 'w') as datafile:
datafile.writelines(newlines)
if CSV_CLEAR_INFILE:
with open(readfilename, 'w') as clearfile:
clearfile.write('')