-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfintel_process_csv.py
More file actions
76 lines (63 loc) · 2.64 KB
/
fintel_process_csv.py
File metadata and controls
76 lines (63 loc) · 2.64 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
#####################################
# Import Modules
#####################################
# Import from Python Standard Library
import pathlib
import csv
import statistics
# Import from local project modules
from utils_logger import logger
#####################################
# Declare Global Variables
#####################################
fetched_folder_name: str = "fintel_solar"
processed_folder_name: str = "fintel_solar_csv_processed"
#####################################
# Define Functions
#####################################
def analyze_solar_acres(file_path: pathlib.Path) -> dict:
"""Analyze the Acres column to calculate min, max, mean, and stdev."""
try:
# initialize an empty list to store the scores
score_list = []
with file_path.open('r') as file:
# csv.DictReader() methods to read into a DictReader so we can access named columns in the csv file
dict_reader = csv.DictReader(file)
for row in dict_reader:
try:
score = float(row["Acres"]) # Extract and convert to float
# append the score to the list
score_list.append(score)
except ValueError as e:
logger.warning(f"Skipping invalid row: {row} ({e})")
# Calculate statistics
stats = {
"min": min(score_list),
"max": max(score_list),
"mean": statistics.mean(score_list),
"stdev": statistics.stdev(score_list) if len(score_list) > 1 else 0,
}
return stats
except Exception as e:
logger.error(f"Error processing CSV file: {e}")
return {}
def process_csv_file():
"""Read a CSV file, analyze Acres, and save the results."""
input_file = pathlib.Path(fetched_folder_name, "fintel_solar")
output_file = pathlib.Path(processed_folder_name, "fintel_solar_csv_processed.txt")
stats = analyze_solar_acres(input_file)
output_file.parent.mkdir(parents=True, exist_ok=True)
with output_file.open('w') as file:
file.write("Solar Acres Statistics:\n")
file.write(f"Minimum: {stats['min']:.2f}\n")
file.write(f"Maximum: {stats['max']:.2f}\n")
file.write(f"Mean: {stats['mean']:.2f}\n")
file.write(f"Standard Deviation: {stats['stdev']:.2f}\n")
logger.info(f"Processed CSV file: {input_file}, Statistics saved to: {output_file}")
#####################################
# Main Execution
#####################################
if __name__ == "__main__":
logger.info("Starting CSV processing...")
process_csv_file()
logger.info("CSV processing complete.")