function create_csv_report
Creates two CSV reports (summary and detailed) from warranty data, writing warranty information to files with different levels of detail.
/tf/active/vicechatdev/convert_disclosures_to_table.py
141 - 165
simple
Purpose
This function generates CSV reports from warranty data structures. It creates two separate files: a summary CSV containing key warranty fields for readability, and a detailed CSV containing all warranty fields including full disclosures. The function is designed for reporting and data export purposes in warranty management systems.
Source Code
def create_csv_report(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'. The first warranty dictionary's keys are used to determine all fields for the detailed CSV.
output_file: Path to the output CSV file for the summary report. Can be a string or Path object. The detailed report will be created in the same directory with '_detailed' appended to the filename before the extension. Must be a valid writable file path.
Return Value
This function returns None. It performs side effects by creating two CSV files on disk: one summary CSV at the specified output_file path, and one detailed CSV at a path derived from output_file with '_detailed' inserted before the '.csv' 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 Warranties',
'Source_Documents_Count': 3,
'Warranty_Text': 'Product meets quality standards',
'Disclosure_Summary': 'Standard quality assurance',
'Full_Disclosure': 'Complete disclosure text here...'
},
{
'Warranty_ID': 'W002',
'Warranty_Title': 'Service Warranty',
'Section_Name': 'Service Warranties',
'Source_Documents_Count': 2,
'Warranty_Text': 'Services performed professionally',
'Disclosure_Summary': 'Professional service guarantee',
'Full_Disclosure': 'Complete service disclosure...'
}
]
# Create CSV reports
create_csv_report(warranties, 'warranty_report.csv')
# This creates 'warranty_report.csv' and 'warranty_report_detailed.csv'
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 logger is properly configured before calling this function
- Check that the output directory exists and has write permissions
- Be aware that the function will overwrite existing files with the same names
- For large datasets, consider memory usage as all warranties are held in memory
- The function uses UTF-8 encoding for CSV files, which is suitable for international characters
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function create_csv_report_improved 95.5% similar
-
function create_excel_report 76.6% similar
-
function create_excel_report_improved 75.4% similar
-
function create_word_report 67.4% similar
-
function main_v15 67.1% similar