-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre-process-midnight.py
executable file
·65 lines (48 loc) · 2.02 KB
/
pre-process-midnight.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
pre-process-midnight.py
Descp: A very simple script that add the missing time part 00:00:00
at midnight for tomst output files in lolly version 1.40.
Created on: 21-dec-2023
Copyright 2023-2024 Abel 'Akronix' Serrano Juste <[email protected]>
LICENSE: GPLv3.0
"""
import os
import csv
import re
import sys
def is_valid_time_format(value):
return re.match(r'.* \d{2}:\d{2}:\d{2}', value) is not None
def add_time_to_second_column(input_file, output_file):
with open(input_file, 'r', newline='') as infile:
reader = csv.reader(infile, delimiter=';')
with open(output_file, 'w', newline='') as outfile:
writer = csv.writer(outfile, delimiter=';')
for row in reader:
if len(row) >= 2 and not is_valid_time_format(row[1]):
row[1] += " 00:00:00"
writer.writerow(row)
print(f"Updated CSV written to {output_file}")
def process_csv_files(input_directory, output_directory):
# Create the output directory if it doesn't exist
if not os.path.exists(output_directory):
os.makedirs(output_directory)
# Iterate through all files in the input directory
for filename in os.listdir(input_directory):
if filename.endswith('.csv'):
input_file = os.path.join(input_directory, filename)
output_file = os.path.join(output_directory, f"{filename}")
add_time_to_second_column(input_file, output_file)
if __name__ == "__main__":
# Check if two directories are provided as command line arguments
if len(sys.argv) != 3:
print("Usage: python pre-process-midnight.py <pre-data-directory> <output-data-directory>")
sys.exit(1)
DATA_DIR = sys.argv[1]
OUTPUT_DATA_DIR = sys.argv[2]
# Check if the provided paths are directories
if not os.path.isdir(DATA_DIR):
print(f"${DATA_DIR} doesn't exist or is not a directory!")
sys.exit(1)
process_csv_files(DATA_DIR, OUTPUT_DATA_DIR)