import csv import json import re def parse_description(description_text): """ Parses the description text into a structured dictionary. """ headers = [ "Background", "Problem Description", "Description", "Impact / Why this problem needs to be solved", "Impact", "Expected Solution", "Expected Outcomes", "Relevant Stakeholders / Beneficiaries", "Supporting Data", "Objective", "Technical Scope", "Data Acquisition & Sampling", "Data Processing & Analysis", "Output & Storage", "Key Performance Parameters", "Eligibility", "Evaluation Criteria", "Deliverables", "Conclusion", "Innovative Features", "Key Features", "Additional Features", "Digital Tourist ID Generation Platform", "Mobile Application for Tourists", "AI-Based Anomaly Detection", "Tourism Department & Police Dashboard", "IoT Integration (Optional)", "Multilingual Support", "Data Privacy & Security" ] pattern = r'^\s*(' + '|'.join(re.escape(h) for h in headers) + r')\s*$' parts = re.split(pattern, description_text, flags=re.MULTILINE) details = {} if parts[0].strip(): details['introduction'] = parts[0].strip() it = iter(parts[1:]) for header in it: content = next(it, "").strip() if header and content: key = header.strip().lower().replace(' ', '_').replace('/', '_').replace('(', '').replace(')', '') details[key] = content if not details: return {'full_text': description_text.strip()} return details def convert_csv_to_unified_json(csv_file_path, json_file_path): """ Converts a CSV to a JSON file containing both the full description and a parsed version. """ problems = [] with open(csv_file_path, mode='r', encoding='utf-8') as csv_file: csv_reader = csv.DictReader(csv_file) for row in csv_reader: cleaned_row = {key.strip().replace(' ', '_').replace('.', ''): value for key, value in row.items()} full_description = cleaned_row.get('Problem_Description', '').strip() problem_data = { 's_no': int(cleaned_row.get('SNo', 0)), 'organization': cleaned_row.get('Organization', ''), 'title': cleaned_row.get('Problem_Statement_Title', ''), 'category': cleaned_row.get('Category', ''), 'ps_number': cleaned_row.get('PS_Number', ''), 'submitted_ideas_count': int(cleaned_row.get('Submitted_Ideas_Count', 0)), 'theme': cleaned_row.get('Theme', ''), 'problem_description': full_description, 'details': parse_description(full_description) } problems.append(problem_data) with open(json_file_path, mode='w', encoding='utf-8') as json_file: json.dump({"problems": problems}, json_file, indent=4) if __name__ == '__main__': csv_input_file = 'SIH_Problem_Statements.csv' json_output_file = 'SIH_Problem_Statements_unified.json' convert_csv_to_unified_json(csv_input_file, json_output_file) print(f"Successfully created unified JSON file: '{json_output_file}'")