commit 9fe87ea60523fcd36d659b291b81241546d9c2b7 Author: Thaddeus Hughes Date: Wed Sep 17 22:24:56 2025 -0500 initial commit. includes sample data works but unpretty diff --git a/LogAB(20).joined.csv b/LogAB(20).joined.csv new file mode 100644 index 0000000..bbb688e --- /dev/null +++ b/LogAB(20).joined.csv @@ -0,0 +1,4 @@ +gross_weight,gross_time,tare_weight,tare_time,net +0,06:12 PM 09/17/25,3780,06:13 PM 09/17/25,-3780 +3780,10:18 PM 09/17/25,3640,10:19 PM 09/17/25,140 +1640,10:19 PM 09/17/25,0,10:20 PM 09/17/25,1640 diff --git a/LogAB(20).sequential.csv b/LogAB(20).sequential.csv new file mode 100644 index 0000000..8eac6f8 --- /dev/null +++ b/LogAB(20).sequential.csv @@ -0,0 +1,7 @@ +weight,timestamp +0,06:12 PM 09/17/25 +3780,06:13 PM 09/17/25 +3780,10:18 PM 09/17/25 +3640,10:19 PM 09/17/25 +1640,10:19 PM 09/17/25 +0,10:20 PM 09/17/25 diff --git a/LogAB(20).txt b/LogAB(20).txt new file mode 100644 index 0000000..745b5ee --- /dev/null +++ b/LogAB(20).txt @@ -0,0 +1,18 @@ +GROSS 00 lb + +06:12 PM 09/17/25 +GROSS 3780 lb + +06:13 PM 09/17/25 +GROSS 3780 lb + +10:18 PM 09/17/25 +GROSS 3640 lb + +10:19 PM 09/17/25 +GROSS 1640 lb + +10:19 PM 09/17/25 +GROSS 00 lb + +10:20 PM 09/17/25 diff --git a/main.py b/main.py new file mode 100644 index 0000000..6578853 --- /dev/null +++ b/main.py @@ -0,0 +1,114 @@ +import re +import csv +import tkinter as tk +from tkinter import filedialog, messagebox +import os + +def parse_logs(log_text): + lines = log_text.splitlines() + if lines and '=~' in lines[0]: + lines = lines[1:] + + pairs = [] + i = 0 + while i < len(lines): + line = lines[i].strip() + if not line: + i += 1 + continue + + match = re.match(r'^GROSS\s+(\d+)\s*lb$', line) + if match: + weight = int(match.group(1)) + i += 2 + if i < len(lines): + ts_line = lines[i].strip() + if re.match(r'^\d{2}:\d{2} (AM|PM) \d{2}/\d{2}/\d{2}$', ts_line): + pairs.append((weight, ts_line)) + i += 1 + else: + i += 1 + else: + i += 1 + + return pairs + +def write_csv1(pairs, filename): + with open(filename, 'w', newline='') as f: + writer = csv.writer(f) + writer.writerow(['weight', 'timestamp']) + for weight, timestamp in pairs: + writer.writerow([weight, timestamp]) + +def write_csv2(pairs, filename): + with open(filename, 'w', newline='') as f: + writer = csv.writer(f) + writer.writerow(['gross_weight', 'gross_time', 'tare_weight', 'tare_time', 'net']) + for j in range(0, len(pairs), 2): + if j + 1 < len(pairs): + gross_weight, gross_time = pairs[j] + tare_weight, tare_time = pairs[j + 1] + net = gross_weight - tare_weight + writer.writerow([gross_weight, gross_time, tare_weight, tare_time, net]) + +def select_input_file(): + filename = filedialog.askopenfilename(filetypes=[("Text files", "*.txt"), ("All files", "*.*")]) + if filename: + input_file_var.set(filename) + # Auto-populate output file names + base = os.path.splitext(filename)[0] + output_file1_var.set(f"{base}.sequential.csv") + output_file2_var.set(f"{base}.joined.csv") + +def select_output_file1(): + filename = filedialog.asksaveasfilename(defaultextension=".csv", filetypes=[("CSV files", "*.csv"), ("All files", "*.*")]) + if filename: + output_file1_var.set(filename) + +def select_output_file2(): + filename = filedialog.asksaveasfilename(defaultextension=".csv", filetypes=[("CSV files", "*.csv"), ("All files", "*.*")]) + if filename: + output_file2_var.set(filename) + +def process_files(): + input_file = input_file_var.get() + output_file1 = output_file1_var.get() + output_file2 = output_file2_var.get() + + if not input_file or not output_file1 or not output_file2: + messagebox.showerror("Error", "Please select all input and output files.") + return + + try: + with open(input_file, 'r') as f: + log_text = f.read() + pairs = parse_logs(log_text) + write_csv1(pairs, output_file1) + write_csv2(pairs, output_file2) + messagebox.showinfo("Success", "CSV files generated successfully!") + except Exception as e: + messagebox.showerror("Error", f"An error occurred: {str(e)}") + +root = tk.Tk() +root.title("Log to CSV Converter") +root.geometry("500x500") + +input_file_var = tk.StringVar() +output_file1_var = tk.StringVar() +output_file2_var = tk.StringVar() + +tk.Label(root, text="Select Input Log File:").pack(pady=5) +tk.Entry(root, textvariable=input_file_var, width=50).pack(pady=5) +tk.Button(root, text="Browse Input", command=select_input_file).pack(pady=5) + +tk.Label(root, text="Select Output CSV (Sequential):").pack(pady=5) +tk.Entry(root, textvariable=output_file1_var, width=50).pack(pady=5) +tk.Button(root, text="Browse Sequential Output", command=select_output_file1).pack(pady=5) + +tk.Label(root, text="Select Output CSV (Joined):").pack(pady=5) +tk.Entry(root, textvariable=output_file2_var, width=50).pack(pady=5) +tk.Button(root, text="Browse Joined Output", command=select_output_file2).pack(pady=5) + +tk.Button(root, text="Convert to CSV", command=process_files).pack(pady=20) + +root.mainloop() \ No newline at end of file