1 00:00:00,700 --> 00:00:02,250 - [Instructor] In this self check exercise, 2 00:00:02,250 --> 00:00:04,740 we'd like you to use the CSV module 3 00:00:04,740 --> 00:00:07,060 that you just saw in the preceding video 4 00:00:07,060 --> 00:00:10,010 to create the grades.csv file 5 00:00:10,010 --> 00:00:14,150 and to write into that file the comma-separated values 6 00:00:14,150 --> 00:00:17,060 that you see here in the problem statement. 7 00:00:17,060 --> 00:00:19,670 Then we'd like you to read that file back in 8 00:00:19,670 --> 00:00:22,810 and display it formatted in three columns 9 00:00:22,810 --> 00:00:25,950 with the column names ID, Name, and Grade. 10 00:00:25,950 --> 00:00:28,310 So go ahead and pause the video and give that a shot, 11 00:00:28,310 --> 00:00:29,960 then come back to see the answer. 12 00:00:34,330 --> 00:00:36,100 Okay, let's go ahead and do this. 13 00:00:36,100 --> 00:00:41,050 We'll first import the CSV module so that we can use it 14 00:00:41,050 --> 00:00:45,190 to write records into a comma-separated value file. 15 00:00:45,190 --> 00:00:48,660 Next, we'll open grades.csv for writing. 16 00:00:48,660 --> 00:00:52,330 Again, there is that newline equals empty string argument. 17 00:00:52,330 --> 00:00:54,000 And we're going to treat this file 18 00:00:54,000 --> 00:00:57,150 as the variable name grades, which we will then use 19 00:00:57,150 --> 00:01:01,120 to create a comma-separated value file writer. 20 00:01:01,120 --> 00:01:02,980 That writer object is then used 21 00:01:02,980 --> 00:01:06,070 to invoke the writerow method once for each 22 00:01:06,070 --> 00:01:09,970 of the three records of information that we are outputting. 23 00:01:09,970 --> 00:01:12,750 Notice again this is a list being passed 24 00:01:12,750 --> 00:01:14,950 into the writerow function. 25 00:01:14,950 --> 00:01:17,300 So let's go ahead and execute that. 26 00:01:17,300 --> 00:01:20,730 And now to read that data back in, 27 00:01:20,730 --> 00:01:22,670 we have a separate with statement. 28 00:01:22,670 --> 00:01:27,670 We're reading from the grades.csv file, open for reading. 29 00:01:28,250 --> 00:01:31,030 Again, newline equals empty string to ensure 30 00:01:31,030 --> 00:01:34,060 that the newline characters are handled correctly. 31 00:01:34,060 --> 00:01:36,960 We print out our formatted column heads 32 00:01:36,960 --> 00:01:41,960 and create a reader to read from the file grades 33 00:01:42,150 --> 00:01:46,010 and we then use a for loop to read one record at a time 34 00:01:46,010 --> 00:01:48,510 from that comma-separated value file. 35 00:01:48,510 --> 00:01:50,950 Each of the records has three pieces of information, 36 00:01:50,950 --> 00:01:53,880 so we split those off from the record 37 00:01:53,880 --> 00:01:55,750 that we're currently processing 38 00:01:55,750 --> 00:02:00,160 and then we format those as three columns of output. 39 00:02:00,160 --> 00:02:04,160 So let's execute that, and you can see indeed the three rows 40 00:02:04,160 --> 00:02:07,800 of data with the column heads as specified back here 41 00:02:07,800 --> 00:02:09,163 in the problem statement.