55 lines
1.3 KiB
Python
55 lines
1.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Read each frame CSV, apply heuristic fixes, overwrite in place."""
|
|
import csv
|
|
import os
|
|
import sys
|
|
|
|
from clean_csv_heuristics import fix_extensao_from_km, fix_row
|
|
|
|
|
|
def read_csv(path):
|
|
with open(path, newline="", encoding="utf-8") as f:
|
|
return list(csv.reader(f))
|
|
|
|
|
|
def write_csv(path, rows):
|
|
with open(path, "w", newline="", encoding="utf-8") as f:
|
|
csv.writer(f).writerows(rows)
|
|
|
|
|
|
def fix_csv(path, num_cols=6):
|
|
rows = read_csv(path)
|
|
fixed = []
|
|
for row in rows:
|
|
if not row:
|
|
fixed.append(row)
|
|
continue
|
|
r = fix_row(row, num_cols)
|
|
r = fix_extensao_from_km(r)
|
|
fixed.append(r)
|
|
write_csv(path, fixed)
|
|
return len(fixed)
|
|
|
|
|
|
def fix_dir(frames_dir, num_cols=6):
|
|
count = 0
|
|
for name in sorted(os.listdir(frames_dir)):
|
|
if not name.endswith(".csv"):
|
|
continue
|
|
path = os.path.join(frames_dir, name)
|
|
try:
|
|
n = fix_csv(path, num_cols)
|
|
count += 1
|
|
except Exception as e:
|
|
print(f"{name}: {e}", file=sys.stderr)
|
|
return count
|
|
|
|
|
|
def main():
|
|
frames_dir = sys.argv[1] if len(sys.argv) > 1 else "frames"
|
|
n = fix_dir(frames_dir)
|
|
print(f"Fixed {n} CSVs in {frames_dir}/")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|