function create_csv_report_improved
Creates two CSV reports from warranty data: a summary report with key fields and a detailed report with all fields including full disclosures.
/tf/active/vicechatdev/improved_convert_disclosures_to_table.py
167 - 191
simple
Purpose
This function generates two CSV files from warranty data for different use cases. The summary CSV contains essential warranty information (ID, title, section, source count, text, and disclosure summary) for quick review and readability. The detailed CSV contains all available warranty fields including full disclosure text for comprehensive analysis. Both files are created with UTF-8 encoding to handle special characters.
Source Code
def create_csv_report_improved(warranties, output_file):
"""Create CSV report from warranty data."""
logger.info(f"Creating CSV report: {output_file}")
# Create summary CSV (without full disclosure for readability)
summary_fields = ['Warranty_ID', 'Warranty_Title', 'Section_Name', 'Source_Documents_Count', 'Warranty_Text', 'Disclosure_Summary']
with open(output_file, 'w', newline='', encoding='utf-8') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=summary_fields)
writer.writeheader()
for warranty in warranties:
# Only write summary fields
summary_row = {field: warranty[field] for field in summary_fields}
writer.writerow(summary_row)
# Create detailed CSV with full disclosures
detailed_output = str(output_file).replace('.csv', '_detailed.csv')
with open(detailed_output, 'w', newline='', encoding='utf-8') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=warranties[0].keys())
writer.writeheader()
writer.writerows(warranties)
logger.info(f"Created summary CSV: {output_file}")
logger.info(f"Created detailed CSV: {detailed_output}")
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
warranties |
- | - | positional_or_keyword |
output_file |
- | - | positional_or_keyword |
Parameter Details
warranties: A list of dictionaries where each dictionary represents a warranty record. Each dictionary must contain at minimum the keys: 'Warranty_ID', 'Warranty_Title', 'Section_Name', 'Source_Documents_Count', 'Warranty_Text', and 'Disclosure_Summary'. Additional keys will be included in the detailed CSV output. Expected to be a non-empty list.
output_file: The file path for the summary CSV output. Can be a string or Path object. The detailed CSV will be created automatically by appending '_detailed' before the '.csv' extension. The directory must exist and be writable.
Return Value
This function returns None (implicitly). It performs side effects by creating two CSV files on disk: the summary CSV at the specified output_file path and a detailed CSV at a path derived from output_file with '_detailed' inserted before the extension.
Dependencies
csvlogging
Required Imports
import csv
import logging
Usage Example
import csv
import logging
# Setup logger
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
# Sample warranty data
warranties = [
{
'Warranty_ID': 'W001',
'Warranty_Title': 'Product Quality Warranty',
'Section_Name': 'General Terms',
'Source_Documents_Count': 3,
'Warranty_Text': 'Product is warranted for 1 year',
'Disclosure_Summary': 'Standard warranty terms apply',
'Full_Disclosure': 'Complete disclosure text here...'
},
{
'Warranty_ID': 'W002',
'Warranty_Title': 'Service Warranty',
'Section_Name': 'Service Terms',
'Source_Documents_Count': 2,
'Warranty_Text': 'Service warranted for 90 days',
'Disclosure_Summary': 'Limited service warranty',
'Full_Disclosure': 'Full service warranty details...'
}
]
# Create CSV reports
create_csv_report_improved(warranties, 'warranty_report.csv')
# This creates:
# - warranty_report.csv (summary with 6 fields)
# - warranty_report_detailed.csv (all fields)
Best Practices
- Ensure the warranties list is not empty before calling this function to avoid IndexError when accessing warranties[0].keys()
- Verify that all warranty dictionaries contain the required summary fields: 'Warranty_ID', 'Warranty_Title', 'Section_Name', 'Source_Documents_Count', 'Warranty_Text', 'Disclosure_Summary'
- Ensure the output directory exists before calling this function
- Use Path objects for cross-platform compatibility when specifying output_file
- The function assumes all warranty dictionaries have the same keys for the detailed CSV; inconsistent keys may result in missing values
- Configure the logger before calling this function to capture informational messages about file creation
- Consider handling potential IOError or PermissionError exceptions when calling this function in production code
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function create_csv_report 95.5% similar
-
function create_excel_report_improved 76.7% similar
-
function create_excel_report 75.6% similar
-
function create_word_report 69.2% similar
-
function main_v15 67.5% similar