From the course: Advanced Hands-On Python: Working with Excel and Spreadsheet Data
Unlock the full course today
Join today to access over 24,600 courses taught by industry experts.
Writing a dictionary as CSV
From the course: Advanced Hands-On Python: Working with Excel and Spreadsheet Data
Writing a dictionary as CSV
- [Instructor] So now that we've seen how to write a CSV file as arrays, in this example, we're going to see how to write a CSV file using dictionaries for the data source instead of arrays. So in this example code, let's open up write_csv_dict.py. And I've already got a list of dictionary objects that represent a subset of the data in the inventory file that we've been using. So you can see that this is a little bit of a different format than we had in the previous example. The first thing I need to do is define the column names that will be the header row for the output CSV file. This is required by the DictWriter object we're going to be using in the CSV module, so it knows how to map each dictionary key to a particular column. So you can see in the sample data that the items in each row consists of a key and a value. The fieldnames parameter will be used to match each of the keys to one of the output columns. And this is required by the DictWriter. This is not optional like it was…
Contents
-
-
-
The CSV format1m 20s
-
Reading CSV files into an array4m 22s
-
(Locked)
Reading CSV files into a dictionary7m 14s
-
(Locked)
Reading CSV files with a filter2m 54s
-
(Locked)
Writing a CSV file2m 34s
-
(Locked)
Writing a dictionary as CSV4m 8s
-
(Locked)
Challenge: Modify CSV content2m 3s
-
(Locked)
Solution: Modify CSV content2m 59s
-
-
-
-
-