-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv_template.py
95 lines (75 loc) · 2.81 KB
/
csv_template.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import csv
import pandas as pd
import re
import sys
from tkinter import Tk, filedialog, Button, Label, Entry, messagebox
import os
# Constants
APP_TITLE = "Title"
DEFAULT_OUTPUT_FILE = 'output.csv'
class CSVProcessor:
def __init__(self, input_file, output_file):
self.input_file = input_file
self.output_file = output_file
def read_csv(self):
with open(self.input_file, mode='r', newline='') as file:
reader = csv.reader(file)
data = [row for row in reader]
return data
def write_csv(self, data):
with open(self.output_file, mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerows(data)
def modify_data(self, data):
# Find the indices of the relevant columns
header = data[0]
# Filter the data based on the new criteria
# Collect some stats
modified_data = filtered_data
return modified_data
def process(self):
try:
# Read the CSV file
data = self.read_csv()
# Modify the data
modified_data = self.modify_data(data)
# Write the modified data to a new CSV file
self.write_csv(modified_data)
return True
except Exception as e:
messagebox.showerror("Error", f"An error occurred during conversion: {e}")
return False
def select_input_file():
global input_file
input_file = filedialog.askopenfilename(title="Select input CSV file", filetypes=[("CSV files", "*.csv")])
if input_file:
input_label.config(text=f"Input File: {input_file}")
def run_conversion():
global output_file
if not input_file:
messagebox.showerror("Error", "Please select an input file.")
return
output_file = os.path.join(os.path.dirname(__file__), output_entry.get())
if not output_file:
messagebox.showerror("Error", "Please specify an output file.")
return
processor = CSVProcessor(input_file, output_file)
if processor.process():
messagebox.showinfo("Done", "Conversion completed successfully!")
os.startfile(os.path.dirname(output_file))
if __name__ == "__main__":
input_file = None
root = Tk()
root.title(APP_TITLE)
input_button = Button(root, text="Select Input File", command=select_input_file)
input_button.pack(pady=10)
input_label = Label(root, text="Input File: None")
input_label.pack(pady=5)
output_label = Label(root, text="Output File:")
output_label.pack(pady=5)
output_entry = Entry(root, width=50)
output_entry.insert(0, DEFAULT_OUTPUT_FILE)
output_entry.pack(pady=5)
convert_button = Button(root, text="Run Conversion", command=run_conversion)
convert_button.pack(pady=20)
root.mainloop()