-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv_to_insights.py
More file actions
32 lines (26 loc) · 913 Bytes
/
csv_to_insights.py
File metadata and controls
32 lines (26 loc) · 913 Bytes
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
import csv
import statistics
import json
try:
values = []
with open('sample-superstore.csv', 'r', encoding='utf-8-sig') as file:
rows = csv.DictReader(file)
# for each row in the CSV file
for row in rows:
try:
values.append(float(row['Sales']))
except ValueError:
continue #Skip the row where the Sales is not a valid number
# Calculate the min, max, mean
if len(values) == 0:
print("No numeric data found.")
result = {
"min": min(values),
"max": max(values),
"mean": statistics.mean(values)
}
with open('results.json', 'w') as json_file:
json.dump(result, json_file, indent=4)
except FileNotFoundError:
print("The file 'sample-superstore.csv' was not found.")
exit()