-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv_writer.py
26 lines (21 loc) · 909 Bytes
/
csv_writer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import csv
def write_csv(filename, header, data):
"""Write the provided data to the CSV file.
:param str filename: The name of the file to which the data should be written.
:param list header: The header for the columns in the csv file.
:param list data: The list of list mapping the values to the columns
"""
try:
with open(filename, 'w', newline='') as csv_file:
csv_writer = csv.DictWriter(csv_file, fieldnames=header)
csv_writer.writeheader()
csv_writer.writerows(data)
except (IOError, OSError) as csv_file_error:
print(f"Unable to write the contents to csv file. Exception: {csv_file_error}")
if __name__ == '__main__':
header = ['name', 'age', 'gender']
data = [{'name': 'Richard', 'age': 32, 'gender': 'M'},
{'name': 'Numzil', 'age': 21, 'gender': 'F'},
{'name': 'Melinda', 'age': 25, 'gender': 'F'}]
filename = 'sample_output.csv'
write_csv(filename, header, data)