-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_isodatetime_in_file_to_timesince.py
70 lines (53 loc) · 2.51 KB
/
convert_isodatetime_in_file_to_timesince.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
67
68
69
70
import csv
import argparse
import datetime
def convert_isodatetime_in_file(input_file, output_file, date_time_column_header):
"""
Import rows from a file, get the ISO datetime and convert it to a timestamp (secs since start of 1970) for one row,
then append the time column to an output file.
"""
missing_values = ['', 'NaN', 'NA', 'NAN']
with open(input_file) as input_csv_file:
csv_reader = csv.reader(input_csv_file, delimiter=',')
with open(output_file, 'w') as output_csv_file:
csv_writer = csv.writer(output_csv_file)
row_count = 0
for row in csv_reader:
if row_count % 10 == 0 and row_count != 0:
print(f'Converted {row_count} rows')
if row_count == 0:
header = row
date_time_column = header.index(date_time_column_header)
header.append('time')
csv_writer.writerow(header)
row_count += 1
continue
row_count += 1
isodatetime = row[date_time_column]
if isodatetime in missing_values:
row.append(isodatetime)
csv_writer.writerow(row)
continue
try:
isodatetime_dt = datetime.datetime.strptime(isodatetime, '%Y-%m-%dT%H:%M:%S%z')
except ValueError:
isodatetime_dt = None
if isodatetime_dt is not None:
secssince1970 = isodatetime_dt.timestamp()
row.append(secssince1970)
csv_writer.writerow(row)
else:
print("Date time not processed: ", isodatetime)
output_csv_file.close()
input_csv_file.close()
print('Total rows converted:', row_count)
def main():
parser = argparse.ArgumentParser(description='Convert ISO datetime to seconds since 1907-01-01T00:00:00Z in '
'INPUT_FILE and write out the new rows to OUTPUT_FILE')
parser.add_argument('input_file', help='Input filepath and name')
parser.add_argument('output_file', help='Output filepath and name')
parser.add_argument('date_time_column_header', help='Header of date_time column in input file')
args = parser.parse_args()
convert_isodatetime_in_file(args.input_file, args.output_file, args.date_time_column_header)
if __name__ == "__main__":
main()