import re import csv import tkinter as tk from tkinter import filedialog, messagebox import os from datetime import datetime 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 # Skip to timestamp line 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): # Convert timestamp to Excel-friendly 12-hour format try: dt = datetime.strptime(ts_line, '%I:%M %p %m/%d/%y') excel_ts = dt.strftime('%m/%d/%Y %I:%M %p') pairs.append((weight, excel_ts)) except ValueError: i += 1 continue i += 1 # Move to next weight entry 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) def process_files(): input_file = input_file_var.get() if not input_file: messagebox.showerror("Error", "Please select an input file.") return # Auto-generate output filenames base = os.path.splitext(input_file)[0] output_file1 = f"{base}.sequential.csv" output_file2 = f"{base}.joined.csv" 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", f"CSV files generated:\n{output_file1}\n{output_file2}") except Exception as e: messagebox.showerror("Error", f"An error occurred: {str(e)}") root = tk.Tk() root.title("Log to CSV Converter") root.geometry("500x120") input_file_var = tk.StringVar() # Input file selection input_frame = tk.Frame(root) input_frame.pack(pady=5, padx=10, fill='x') tk.Label(input_frame, text="Select Input Log File:").pack(side='left') tk.Entry(input_frame, textvariable=input_file_var, width=40).pack(side='left', padx=5) tk.Button(input_frame, text="Browse Input", command=select_input_file).pack(side='left') # Convert button tk.Button(root, text="CONVERT TO CSV", command=process_files, font=("Arial", 14), relief="raised", bg="#4CAF50", fg="white").pack(pady=20, padx=10, fill='x') root.mainloop()