| name | run2_csv-reporting |
| description | Reliable CSV generation for security audits with automated field mapping. |
Reliable CSV Reporting
This skill focuses on clean and error-free CSV generation.
Best Practices
- Use
csv.DictWriter for clarity and automatic field alignment.
- Always specify
newline='' when opening the file to avoid blank lines on some platforms.
- Ensure all dictionary keys match the
fieldnames list exactly.
import csv
def generate_csv_report(data_list, output_file):
if not data_list:
print("No data to write to CSV.")
return
fieldnames = list(data_list[0].keys())
with open(output_file, 'w', newline='', encoding='utf-8') as f:
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(data_list)