1 00:00:00,005 --> 00:00:02,004 - [Instructor] Being able to read CSV files 2 00:00:02,004 --> 00:00:03,008 is only half the equation. 3 00:00:03,008 --> 00:00:07,002 We need to be able to write CSV data as well. 4 00:00:07,002 --> 00:00:09,006 The CSV module provides a set of classes 5 00:00:09,006 --> 00:00:12,005 and functions for doing exactly that. 6 00:00:12,005 --> 00:00:14,001 So in this example, we're going to see 7 00:00:14,001 --> 00:00:17,005 how to write data that is in an array format, 8 00:00:17,005 --> 00:00:19,001 and we'll see how to write dictionary data 9 00:00:19,001 --> 00:00:21,003 in a separate video. 10 00:00:21,003 --> 00:00:26,001 So for this example, let's open up write_csv_array.py. 11 00:00:26,001 --> 00:00:27,009 And for this example, you can see 12 00:00:27,009 --> 00:00:29,001 that I've already got some code 13 00:00:29,001 --> 00:00:32,002 that defines an array of rows of data. 14 00:00:32,002 --> 00:00:34,007 The first row is the header information, 15 00:00:34,007 --> 00:00:37,005 and the rest of the rows contain lists 16 00:00:37,005 --> 00:00:39,003 of different food types and prices. 17 00:00:39,003 --> 00:00:42,000 It's essentially a subset of the inventory file 18 00:00:42,000 --> 00:00:44,005 that we've been working with in the chapter. 19 00:00:44,005 --> 00:00:46,005 So let's write a function 20 00:00:46,005 --> 00:00:49,008 that will write the data to a CSV file. 21 00:00:49,008 --> 00:00:53,007 So let's declare the function, 22 00:00:53,007 --> 00:01:00,005 and we'll call it write_array_to_csv. 23 00:01:00,005 --> 00:01:02,006 And that will take in the data to write 24 00:01:02,006 --> 00:01:05,007 along with a filename. 25 00:01:05,007 --> 00:01:07,002 Then all we need to do 26 00:01:07,002 --> 00:01:09,007 is open the file that we want to write to. 27 00:01:09,007 --> 00:01:12,004 So I'll use my with open statement, 28 00:01:12,004 --> 00:01:13,007 and I'll pass in the filename. 29 00:01:13,007 --> 00:01:16,003 And this time I'm going to use write mode. 30 00:01:16,003 --> 00:01:17,009 And I'm going to specify the new line 31 00:01:17,009 --> 00:01:20,003 is equal to an empty space, 32 00:01:20,003 --> 00:01:22,009 which means write out the line endings 33 00:01:22,009 --> 00:01:27,002 according to what your operating system prefers. 34 00:01:27,002 --> 00:01:32,004 And I'll open that as csvfile. 35 00:01:32,004 --> 00:01:34,005 And then we just need to write the data. 36 00:01:34,005 --> 00:01:37,006 So I'm going to make a variable called writer, 37 00:01:37,006 --> 00:01:41,007 and I'll create a csv.writer object 38 00:01:41,007 --> 00:01:46,000 with the CSV file I just opened. 39 00:01:46,000 --> 00:01:49,006 And using that writer, I'm going to call writerows 40 00:01:49,006 --> 00:01:53,000 to write multiple rows of data. 41 00:01:53,000 --> 00:01:55,003 And that's pretty much all there is to it. 42 00:01:55,003 --> 00:01:58,000 You can see it's a little easier than reading the data. 43 00:01:58,000 --> 00:02:00,007 So all we need to do now is call that function. 44 00:02:00,007 --> 00:02:04,005 So let's call write_array_to_csv, 45 00:02:04,005 --> 00:02:06,003 and we'll pass in my data array, 46 00:02:06,003 --> 00:02:08,009 which is defined right here. 47 00:02:08,009 --> 00:02:10,005 And we'll give it a name. 48 00:02:10,005 --> 00:02:15,004 We'll call it output.csv. 49 00:02:15,004 --> 00:02:17,009 All right, so let's save 50 00:02:17,009 --> 00:02:21,009 and let's try running this. 51 00:02:21,009 --> 00:02:23,003 All right, and when I run this, 52 00:02:23,003 --> 00:02:25,001 you can see that over here in the sidebar 53 00:02:25,001 --> 00:02:28,004 a little output.csv file has shown up. 54 00:02:28,004 --> 00:02:30,004 So let's click on it. 55 00:02:30,004 --> 00:02:34,000 And sure enough, it contains the data from the array.