import csv import json import re def parse_description(description_text): """ Parses the description text into a structured dictionary. """ # Headers to look for in the description text. # The order is important for splitting. 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" ] # Create a regex pattern to split the text by the headers. # The pattern looks for a header at the beginning of a line. pattern = r'^\s*(' + '|'.join(re.escape(h) for h in headers) + r')\s*$' # Split the text by the headers parts = re.split(pattern, description_text, flags=re.MULTILINE) details = {} # The first part is the introduction if it's not a header if parts[0].strip(): details['introduction'] = parts[0].strip() # The rest of the parts are pairs of (header, content) it = iter(parts[1:]) for header in it: content = next(it, "").strip() if header and content: # Normalize header to be used as a json key key = header.strip().lower().replace(' ', '_').replace('/', '_').replace('(', '').replace(')', '') details[key] = content # If details is empty, it means no headers were found. # In this case, the whole text is the description. if not details: return {'full_text': description_text.strip()} return details def convert_csv_to_json(csv_file_path, json_file_path): """ Converts a CSV file to a structured JSON file. """ problems = [] with open(csv_file_path, mode='r', encoding='utf-8') as csv_file: # Use DictReader to read CSV rows as dictionaries csv_reader = csv.DictReader(csv_file) for row in csv_reader: # Clean up keys from the CSV header cleaned_row = {key.strip().replace(' ', '_').replace('.', ''): value for key, value in row.items()} 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', ''), 'details': parse_description(cleaned_row.get('Problem_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=2) if __name__ == '__main__': csv_input_file = 'SIH_Problem_Statements.csv' json_output_file = 'SIH_Problem_Statements_structured.json' convert_csv_to_json(csv_input_file, json_output_file) print(f"Successfully converted '{csv_input_file}' to '{json_output_file}'")