1 00:00:00,005 --> 00:00:01,003 - [Instructor] So now that we've seen 2 00:00:01,003 --> 00:00:04,008 how to write a CSV file as arrays, in this example, 3 00:00:04,008 --> 00:00:06,009 we're going to see how to write a CSV file 4 00:00:06,009 --> 00:00:11,006 using dictionaries for the data source instead of arrays. 5 00:00:11,006 --> 00:00:17,005 So in this example code, let's open up write_csv_dict.py. 6 00:00:17,005 --> 00:00:21,004 And I've already got a list of dictionary objects 7 00:00:21,004 --> 00:00:23,008 that represent a subset of the data 8 00:00:23,008 --> 00:00:26,007 in the inventory file that we've been using. 9 00:00:26,007 --> 00:00:28,006 So you can see that this is a little bit 10 00:00:28,006 --> 00:00:32,002 of a different format than we had in the previous example. 11 00:00:32,002 --> 00:00:36,000 The first thing I need to do is define the column names 12 00:00:36,000 --> 00:00:40,006 that will be the header row for the output CSV file. 13 00:00:40,006 --> 00:00:44,001 This is required by the DictWriter object we're going 14 00:00:44,001 --> 00:00:47,007 to be using in the CSV module, so it knows how 15 00:00:47,007 --> 00:00:52,000 to map each dictionary key to a particular column. 16 00:00:52,000 --> 00:00:53,005 So you can see in the sample data 17 00:00:53,005 --> 00:00:59,005 that the items in each row consists of a key and a value. 18 00:00:59,005 --> 00:01:03,005 The fieldnames parameter will be used to match 19 00:01:03,005 --> 00:01:08,006 each of the keys to one of the output columns. 20 00:01:08,006 --> 00:01:10,009 And this is required by the DictWriter. 21 00:01:10,009 --> 00:01:14,002 This is not optional like it was in the reading example. 22 00:01:14,002 --> 00:01:22,000 So what I'm going to do is create a fieldnames variable. 23 00:01:22,000 --> 00:01:26,004 And that's going to be a list of the keys in my data. 24 00:01:26,004 --> 00:01:36,001 So I'm going to have Item Name, Category, Quantity. 25 00:01:36,001 --> 00:01:41,005 And then we'll have the Wholesale Price 26 00:01:41,005 --> 00:01:43,009 and the Consumer Price. 27 00:01:43,009 --> 00:01:46,007 And these need to match each of the keys 28 00:01:46,007 --> 00:01:48,004 that you have in your dictionary. 29 00:01:48,004 --> 00:01:50,007 So make sure that they're spelled correctly, 30 00:01:50,007 --> 00:01:54,000 otherwise it's going to cause a problem. 31 00:01:54,000 --> 00:01:56,008 All right, so now let's build our function 32 00:01:56,008 --> 00:02:01,001 to write the CSV file. 33 00:02:01,001 --> 00:02:06,002 So I'm going to call that write_dict_to_csv. 34 00:02:06,002 --> 00:02:10,004 And once again, that will take the data and the filename. 35 00:02:10,004 --> 00:02:11,008 So similar to the previous example, 36 00:02:11,008 --> 00:02:17,001 we will open our file for writing. 37 00:02:17,001 --> 00:02:22,005 So we'll open the filename in write mode with line endings. 38 00:02:22,005 --> 00:02:27,004 Oops. 39 00:02:27,004 --> 00:02:32,001 And we'll open that as CSV file. 40 00:02:32,001 --> 00:02:34,009 Only this time, we're going to use a DictWriter 41 00:02:34,009 --> 00:02:35,008 to write the data. 42 00:02:35,008 --> 00:02:38,001 And this is where we supply the field names 43 00:02:38,001 --> 00:02:39,006 that we've created. 44 00:02:39,006 --> 00:02:42,003 So I'm going to create a writer variable 45 00:02:42,003 --> 00:02:48,003 and that's going to be an instance of the DictWriter class. 46 00:02:48,003 --> 00:02:53,002 And I'm going to pass in the CSV file that we created, 47 00:02:53,002 --> 00:02:55,007 and I'm going to specify that fieldnames is equal 48 00:02:55,007 --> 00:03:00,002 to the fieldnames variable that we've created. 49 00:03:00,002 --> 00:03:02,005 And then we'll just write the data as usual. 50 00:03:02,005 --> 00:03:06,002 So we'll call writer, 51 00:03:06,002 --> 00:03:11,005 and I'm going to call writeheader first. 52 00:03:11,005 --> 00:03:14,008 That will output all the field names as the header row. 53 00:03:14,008 --> 00:03:20,001 And then I'll call writer.writerows, 54 00:03:20,001 --> 00:03:25,008 and we'll pass in the array of dictionaries. 55 00:03:25,008 --> 00:03:28,006 And once again, I just need to call the function 56 00:03:28,006 --> 00:03:29,009 for all this to work. 57 00:03:29,009 --> 00:03:32,007 So let's go ahead and call our function 58 00:03:32,007 --> 00:03:34,008 and we'll pass in the data. 59 00:03:34,008 --> 00:03:40,007 And once again we'll call it output.csv. 60 00:03:40,007 --> 00:03:42,004 All right, so let's save, that should about do it. 61 00:03:42,004 --> 00:03:44,009 Let's run our code. 62 00:03:44,009 --> 00:03:48,003 So we'll run this in the terminal, 63 00:03:48,003 --> 00:03:52,008 and when I run it, we can see output.csv was created 64 00:03:52,008 --> 00:03:54,007 and everything looks good. 65 00:03:54,007 --> 00:03:55,006 Take a look at that. 66 00:03:55,006 --> 00:03:57,003 Sure enough, CSV data looks right. 67 00:03:57,003 --> 00:04:00,009 We've got our header row, Item Name, Category, Quantity, 68 00:04:00,009 --> 00:04:04,009 Wholesale, Consumer Price, along with all of the values 69 00:04:04,009 --> 00:04:08,000 that we wrote out in our dictionaries.