Writing Files Exercise
Question 1 of 4
When writing a list of data to a CSV file, which approach correctly formats the output?
data = [
['Name', 'Age', 'City'],
['Alice', 28, 'New York'],
['Bob', 35, 'Los Angeles']
]
# Write to CSV filewith open('data.csv', 'w') as f: f.write(str(data))
with open('data.csv', 'w') as f: f.writelines(data)
with open('data.csv', 'w') as f: \n for row in data: f.write(' '.join(row) + '\n')
with open('data.csv', 'w') as f: \n for row in data: f.write(','.join(map(str, row)) + '\n')