import tkinter as tk
from tkinter import filedialog, messagebox, ttk
import pandas as pd
from datetime import datetime
import os
import threading

def get_log_filename():
    now = datetime.now()
    return f"process_log_{now.strftime('%Y%m%d_%H%M%S')}.txt"

log_filename = get_log_filename()
log_timer = None

def clear_log():
    log_text.delete(1.0, tk.END)

def log_message(message):
    global log_timer
    with open(log_filename, "a") as log_file:
        log_file.write(message + "\n")
    log_text.insert(tk.END, message + "\n")
    log_text.see(tk.END)
    
    if log_timer:
        root.after_cancel(log_timer)
    log_timer = root.after(60000, clear_log)

def modify_files():
    year = year_entry.get()
    month = month_entry.get()
    day = day_entry.get()

    if not (year.isdigit() and month.isdigit() and day.isdigit()):
        messagebox.showerror("输入错误", "年、月、日必须是数字")
        return

    year = int(year)
    month = int(month)
    day = int(day)

    if not (2021 <= year <= 2024 and 1 <= month <= 12 and 1 <= day <= 31):
        messagebox.showerror("输入错误", "年、月、日值不在有效范围内")
        return

    files = filedialog.askopenfilenames(filetypes=[("CSV files", "*.csv")])
    if not files:
        messagebox.showwarning("未选择文件", "请选择至少一个文件")
        return

    progress_bar["maximum"] = len(files)
    progress_bar["value"] = 0

    thread = threading.Thread(target=process_files, args=(files, year, month, day))
    thread.start()

def process_files(files, year, month, day):
    for index, file in enumerate(files):
        try:
            df = pd.read_csv(file, encoding='utf-8', on_bad_lines='skip', memory_map=True)
        except UnicodeDecodeError:
            try:
                df = pd.read_csv(file, encoding='latin1', on_bad_lines='skip', memory_map=True)
            except Exception as e:
                log_message(f"文件 {file} 读取失败: {e}")
                continue
        except Exception as e:
            log_message(f"文件 {file} 读取失败: {e}")
            continue

        try:
            log_message(f"文件 {file} 的第一行内容: {df.columns[0]}")
            if df.columns[0].strip() != "WRITE_TIME":
                raise ValueError(f"第一行不是 'WRITE_TIME',而是 '{df.columns[0]}'")

            # 向量化处理时间戳
            timestamps = df.iloc[:, 0]
            valid_timestamps = timestamps.apply(lambda x: isinstance(x, str) and x.startswith("[") and x.endswith("]"))
            if not valid_timestamps.all():
                invalid_row = valid_timestamps.idxmax() + 1
                log_message(f"识别到表格尾部 第 {invalid_row} 行,结束修改")
                timestamps = timestamps[valid_timestamps]

            try:
                datetimes = pd.to_datetime(timestamps.str[1:-1], format="%Y-%m-%d %H:%M:%S.%f")
                new_dates = datetimes.apply(lambda dt: dt.replace(year=year, month=month, day=day))
                new_timestamps = "[" + new_dates.dt.strftime("%Y-%m-%d %H:%M:%S.%f").str[:-3] + "]"
                df.iloc[:len(new_timestamps), 0] = new_timestamps
                log_message(f"文件 {file} 的时间戳已修改")

                df.to_csv(file, index=False, encoding='utf-8')

                # 获取A2单元格和A列最后一行的时间
                creation_time_str = new_timestamps.iloc[1]
                modification_time_str = new_timestamps.iloc[-1]

                creation_time = datetime.strptime(creation_time_str, "[%Y-%m-%d %H:%M:%S.%f]").timestamp()
                modification_time = datetime.strptime(modification_time_str, "[%Y-%m-%d %H:%M:%S.%f]").timestamp()
                os.utime(file, (modification_time, creation_time))

                log_message(f"文件 {file} 修改成功")
            except Exception as e:
                log_message(f"无法解析文件 {file} 的时间戳: {e}")
                continue

        except Exception as e:
            log_message(f"文件 {file} 修改失败: {e}")

        # 更新进度条和日志
        progress_bar["value"] = index + 1
        log_message(f"已处理 {index + 1} / {len(files)} 个文件")

root = tk.Tk()
root.title("CSV 文件修改工具")

tk.Label(root, text="年 (2021-2024):").grid(row=0, column=0, padx=5, pady=5)
year_entry = tk.Entry(root)
year_entry.grid(row=0, column=1, padx=5, pady=5)

tk.Label(root, text="月 (01-12):").grid(row=1, column=0, padx=5, pady=5)
month_entry = tk.Entry(root)
month_entry.grid(row=1, column=1, padx=5, pady=5)

tk.Label(root, text="日 (01-31):").grid(row=2, column=0, padx=5, pady=5)
day_entry = tk.Entry(root)
day_entry.grid(row=2, column=1, padx=5, pady=5)

tk.Button(root, text="选择文件并修改", command=modify_files).grid(row=3, column=0, columnspan=2, padx=5, pady=5)

progress_bar = ttk.Progressbar(root, orient="horizontal", length=400, mode="determinate")
progress_bar.grid(row=4, column=0, columnspan=2, padx=5, pady=5)

log_text = tk.Text(root, height=10, width=50)
log_text.grid(row=5, column=0, columnspan=2, padx=5, pady=5)

root.mainloop()
最后修改:2024 年 05 月 15 日
如果觉得我的文章对你有用,请随意赞赏