-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconverting_to_json.py
More file actions
58 lines (51 loc) · 2.71 KB
/
converting_to_json.py
File metadata and controls
58 lines (51 loc) · 2.71 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
import re
import json
def parse_and_generate_json():
"""
Parses 'email_summary_report.txt' and generates 'report.json'.
This script should be run from the same directory where your report text file is located.
"""
try:
report_data = []
current_entry = None
# Ensure the file is read with utf-8 encoding to handle various characters
with open('email_summary_report.txt', 'r', encoding='utf-8') as f:
for line in f:
line = line.strip()
# Skip empty lines or the main header
if not line or "Processed Emails Report" in line:
continue
# A new entry starts with '---'
if line.startswith('---'):
# If there's a previously processed entry, save it to our list
if current_entry:
report_data.append(current_entry)
# Start a new entry
current_entry = {'title': line.strip(' -'), 'details': []}
# Check if 'from' field has not been assigned yet and the line contains 'From:'
elif 'from' not in current_entry and line.startswith('From:'):
from_match = re.search(r"From: (.*?) \|", line)
date_match = re.search(r"Date: (.*)", line)
if from_match:
current_entry['from'] = from_match.group(1).strip()
if date_match:
current_entry['date'] = date_match.group(1).strip()
# Check if 'summary' has not been assigned and the line starts with 'Summary:'
elif 'summary' not in current_entry and line.startswith('Summary:'):
current_entry['summary'] = line.replace('Summary:', '').strip()
# Lines starting with '- ' are considered details or action items
elif line.startswith('- '):
current_entry['details'].append(line.lstrip('- ').strip())
# Append the very last entry in the file
if current_entry:
report_data.append(current_entry)
# Write the structured data to a JSON file
with open('report.json', 'w', encoding='utf-8') as json_file:
json.dump(report_data, json_file, indent=4)
print("Successfully generated report.json. You can now view it on the dashboard.")
except FileNotFoundError:
print("Error: email_summary_report.txt not found. Make sure it's in the same directory.")
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
parse_and_generate_json()