#100DaysOfCode in Python Transcripts
Chapter: Days 37-39: Using CSV data
Lecture: Concepts: CSV programming

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Recall there were two basic steps that we had to go through to work with this CSV data, and they were both pretty simple.
0:07 We're going to start by importing the csv module, so that will let us actually create this dictionary reader to run through it,
0:15 and we're going to open a file stream that we hand to the dictionary reader to actually get the data.
0:22 Then we just create one of these dictionary readers based on the file, and gives us this thing that we can loop over
0:29 and each item that comes out of the loop is going to be one of those rows in there. And we get this row back and we work with it one at a time.
0:38 Now you saw the data's always text, so you may need to upgrade it to something more usable if you're working with numbers and dates
0:45 and things that are not just plain text. The other thing that makes this really, really nice,
0:50 makes a big difference in terms of working with this data, is to give it a little more structure, and we saw that we could really easily use
0:59 something called a namedtuple. This comes out of the collections and then we're going to create the namedtuple here and we call it whatever you want,
1:09 we make up the name. I said I'm going to call it record, so record it is. Just remember it appears in two places,
1:14 and in that second part, we actually put the basically the header from the CSV, it just so happens that format works perfectly there.
1:23 And then we can, if we have some data, we can create one of the by setting the keyword values. So we set all the values there.
1:30 We actually have a shorter way to do it if the dictionary exactly matches the items, which it does in this case. So we could use the **row,
1:38 because often there's a ton of keyword values to set, and this will just do it in a super, nice shortcut here.


Talk Python's Mastodon Michael Kennedy's Mastodon