function save_results
Saves comparison results data to both CSV and JSON file formats with predefined field structure and UTF-8 encoding.
/tf/active/vicechatdev/mailsearch/compare_documents.py
324 - 352
simple
Purpose
This function persists comparison results (typically from file or document comparison operations) to disk in two formats: CSV for tabular analysis and JSON for structured data interchange. It uses a fixed schema with 14 fields including document codes, file metadata (size, hash), match status, and similarity metrics. The function provides console feedback upon successful save operations.
Source Code
def save_results(results: List[Dict], csv_path: str, json_path: str):
"""
Save comparison results to CSV and JSON
Args:
results: List of comparison results
csv_path: Path to CSV output file
json_path: Path to JSON output file
"""
# Save CSV
fieldnames = [
'document_code', 'status', 'match_type',
'output_filename', 'output_size', 'output_hash',
'wuxi2_filename', 'wuxi2_path', 'wuxi2_size', 'wuxi2_hash',
'size_match', 'hash_match', 'filename_similarity', 'notes'
]
with open(csv_path, 'w', encoding='utf-8', newline='') as f:
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(results)
print(f"\nā CSV results saved to: {csv_path}")
# Save JSON
with open(json_path, 'w', encoding='utf-8') as f:
json.dump(results, f, indent=2, ensure_ascii=False)
print(f"ā JSON results saved to: {json_path}")
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
results |
List[Dict] | - | positional_or_keyword |
csv_path |
str | - | positional_or_keyword |
json_path |
str | - | positional_or_keyword |
Parameter Details
results: A list of dictionaries where each dictionary represents a comparison result. Each dictionary should contain keys matching the CSV fieldnames: 'document_code', 'status', 'match_type', 'output_filename', 'output_size', 'output_hash', 'wuxi2_filename', 'wuxi2_path', 'wuxi2_size', 'wuxi2_hash', 'size_match', 'hash_match', 'filename_similarity', and 'notes'. Missing keys will result in empty CSV cells.
csv_path: String representing the file system path where the CSV output file will be written. Should include the .csv extension. Parent directories must exist or an error will be raised. Existing files will be overwritten.
json_path: String representing the file system path where the JSON output file will be written. Should include the .json extension. Parent directories must exist or an error will be raised. Existing files will be overwritten.
Return Value
This function returns None (implicitly). It performs side effects by writing files to disk and printing status messages to stdout. Success is indicated by console output messages prefixed with 'ā'.
Dependencies
csvjson
Required Imports
import csv
import json
from typing import List, Dict
Usage Example
import csv
import json
from typing import List, Dict
def save_results(results: List[Dict], csv_path: str, json_path: str):
fieldnames = [
'document_code', 'status', 'match_type',
'output_filename', 'output_size', 'output_hash',
'wuxi2_filename', 'wuxi2_path', 'wuxi2_size', 'wuxi2_hash',
'size_match', 'hash_match', 'filename_similarity', 'notes'
]
with open(csv_path, 'w', encoding='utf-8', newline='') as f:
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(results)
print(f"\nā CSV results saved to: {csv_path}")
with open(json_path, 'w', encoding='utf-8') as f:
json.dump(results, f, indent=2, ensure_ascii=False)
print(f"ā JSON results saved to: {json_path}")
# Example usage
comparison_results = [
{
'document_code': 'DOC001',
'status': 'matched',
'match_type': 'exact',
'output_filename': 'file1.pdf',
'output_size': 1024,
'output_hash': 'abc123',
'wuxi2_filename': 'file1.pdf',
'wuxi2_path': '/path/to/file1.pdf',
'wuxi2_size': 1024,
'wuxi2_hash': 'abc123',
'size_match': True,
'hash_match': True,
'filename_similarity': 1.0,
'notes': 'Perfect match'
}
]
save_results(comparison_results, 'output/results.csv', 'output/results.json')
Best Practices
- Ensure all dictionaries in the results list contain the expected keys to avoid missing data in CSV output
- Create parent directories before calling this function using os.makedirs() or Path.mkdir()
- Use absolute paths or properly resolve relative paths to avoid file location issues
- Consider wrapping calls in try-except blocks to handle IOError, PermissionError, or disk space issues
- The function overwrites existing files without warning - implement backup logic if needed
- For large result sets, consider streaming writes or chunking to manage memory usage
- The ensure_ascii=False parameter in json.dump preserves Unicode characters - ensure downstream consumers support UTF-8
- CSV newline='' parameter prevents extra blank lines on Windows systems
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function save_results_v1 81.8% similar
-
function export_results 67.1% similar
-
function print_summary 62.3% similar
-
function generate_failure_report 54.8% similar
-
function save_document_to_file 52.8% similar