-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
169 lines (126 loc) · 4 KB
/
main.py
File metadata and controls
169 lines (126 loc) · 4 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import csv
import os
import tkinter as tk
from tkinter import filedialog
from rich.console import Console
from rich.table import Table
from rich.prompt import Prompt
from rich import box
console = Console()
def load_csv(file_path):
"""
Load CSV file and return headers and rows
"""
with open(file_path, newline='', encoding="utf-8") as f:
reader = csv.DictReader(f)
headers = reader.fieldnames
rows = list(reader)
return headers, rows
def get_csv_info(headers, rows):
"""
Return row count, column count, and column list
"""
data_count = len(rows)
column_count = len(headers)
return data_count, column_count, headers
def filter_and_sort_data(rows, column, order):
"""
Filter and sort data based on column and order
"""
try:
sorted_data = sorted(
rows,
key=lambda x: x[column],
reverse=True if order == "descending" else False
)
return sorted_data
except KeyError:
console.print("[red]Invalid column selected![/red]")
return rows
def print_table(headers, rows, title="CSV Data Preview"):
"""
Print CSV data nicely using rich
"""
table = Table(
title=title,
show_lines=True,
box=box.SIMPLE_HEAVY
)
for header in headers:
table.add_column(header, style="cyan", no_wrap=True)
for row in rows:
table.add_row(*(str(row[h]) for h in headers))
console.print(table)
def print_csv_summary(data_count, column_count, headers):
"""
Print CSV metadata
"""
console.print("\n[bold green]CSV Summary[/bold green]")
console.print(f"📄 Rows : {data_count}")
console.print(f"📊 Columns : {column_count}")
console.print("\n[bold yellow]Available Columns[/bold yellow]")
for idx, col in enumerate(headers, start=1):
console.print(f"{idx}. {col}")
def save_csv(headers, rows):
"""
Save filtered data to user selected directory
"""
root = tk.Tk()
root.withdraw()
folder = filedialog.askdirectory(title="Select Folder to Save CSV")
if not folder:
console.print("[red]Save cancelled[/red]")
return
file_path = os.path.join(folder, "filtered_data.csv")
with open(file_path, "w", newline="", encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=headers)
writer.writeheader()
writer.writerows(rows)
console.print(f"[bold green]Saved successfully at:[/bold green] {file_path}")
def main():
console.print("[bold cyan]CSV Analytics Tool[/bold cyan]\n")
# File selection
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename(
title="Select CSV File",
filetypes=[("CSV Files", "*.csv")]
)
if not file_path:
console.print("[red]No file selected. Exiting...[/red]")
return
console.print(f"[green]Loaded file:[/green] {file_path}\n")
# Load CSV
headers, rows = load_csv(file_path)
# CSV info
data_count, column_count, headers = get_csv_info(headers, rows)
print_csv_summary(data_count, column_count, headers)
# User choices
selected_column = Prompt.ask(
"\nSelect column for comparison",
choices=headers
)
order = Prompt.ask(
"Select order",
choices=["ascending", "descending"],
default="ascending"
)
console.print(
f"\n[bold blue]Selected Column:[/bold blue] {selected_column}"
f"\n[bold blue]Order:[/bold blue] {order}\n"
)
# Filter & sort
filtered_data = filter_and_sort_data(rows, selected_column, order)
# Display result
print_table(headers, filtered_data, title="Filtered & Sorted Data")
# Save option
save_choice = Prompt.ask(
"\nDo you want to save the filtered data?",
choices=["yes", "no"],
default="no"
)
if save_choice == "yes":
save_csv(headers, filtered_data)
console.print("\n[bold green]Done ✔[/bold green]")
if __name__ == "__main__":
main()