-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_data.py
More file actions
164 lines (117 loc) · 4.78 KB
/
Copy pathprocess_data.py
File metadata and controls
164 lines (117 loc) · 4.78 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
import csv
import json
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class DataProcessor:
def __init__(self, input_file):
self.input_file = input_file
self.customers = {}
self.transactions = []
self.reports = {}
def load_data(self):
try:
with open(self.input_file, 'r') as file:
reader = csv.DictReader(file)
for row in reader:
self.customers[row['customer_id']] = {
'name': row['name'],
'email': row['email'],
'join_date': row['join_date'],
'total_spent': 0,
'transaction_count': 0
}
logger.info(f"Loaded {len(self.customers)} customers")
return True
except Exception as e:
logger.error(f"Error loading customer data: {e}")
return False
def process_transactions(self, transaction_file):
try:
with open(transaction_file, 'r') as file:
reader = csv.DictReader(file)
for row in reader:
transaction = {
'transaction_id': row['transaction_id'],
'customer_id': row['customer_id'],
'amount': float(row['amount']),
'date': row['date'],
'category': row['category']
}
self.transactions.append(transaction)
customer = self.customers.get(row['customer_id'])
if customer:
customer['total_spent'] += float(row['amount'])
customer['transaction_count'] += 1
logger.info(f"Processed {len(self.transactions)} transactions")
return True
except Exception as e:
logger.error(f"Error processing transactions: {e}")
return False
def calculate_customer_metrics(self):
total_revenue = sum(
transaction['amount'] for transaction in self.transactions
)
metrics = {
'total_customers': len(self.customers),
'total_transactions': len(self.transactions),
'total_revenue': total_revenue,
'average_transaction_value': (
total_revenue / len(self.transactions)
if self.transactions else 0
),
'top_customers': sorted(
self.customers.items(),
key=lambda x: x[1]['total_spent'],
reverse=True
)[:5]
}
return metrics
def export_customer_data(self, output_file, format='csv'):
try:
if not self.customers:
logger.warning("No customer data available for export")
return False
if format == 'csv':
valid_customers = {}
for customer_id, data in self.customers.items():
if isinstance(data, dict):
valid_customers[customer_id] = data
else:
logger.warning(
f"Skipping malformed customer record: {customer_id}"
)
if not valid_customers:
logger.error("No valid customer records found")
return False
first_customer = next(iter(valid_customers.values()))
fieldnames = ['customer_id'] + list(first_customer.keys())
with open(output_file, 'w', newline='') as file:
writer = csv.DictWriter(
file,
fieldnames=fieldnames
)
writer.writeheader()
rows = [
{'customer_id': cid, **data}
for cid, data in valid_customers.items()
]
writer.writerows(rows)
elif format == 'json':
with open(output_file, 'w') as file:
json.dump(self.customers, file, indent=2)
else:
logger.error(f"Unsupported format: {format}")
return False
logger.info(f"Exported customer data to {output_file}")
return True
except Exception as e:
logger.error(f"Error exporting data: {e}")
return False
if __name__ == "__main__":
processor = DataProcessor("customers.csv")
processor.load_data()
processor.process_transactions("transactions.csv")
metrics = processor.calculate_customer_metrics()
print(metrics)
processor.export_customer_data("customer_export.csv")