in python how do you write a dict to csv
- User
in python how do you write a dict to csv
Assistant
You can write a dictionary to a CSV file in Python using the
csvmodule. Here's an example of how you can do it:import csv # Create a dictionary to write to the CSV file my_dict = { 'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35], 'Gender': ['Female', 'Male', 'Male'] } # Open a file in write mode and create a CSV writer with open('data.csv', 'w', newline='') as csvfile: writer = csv.DictWriter(csvfile, fieldnames=my_dict.keys()) # Write the header row writer.writeheader() # Write each row of data for row in my_dict.values(): writer.writerow(row)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.