#100DaysOfCode in Python Transcripts
Chapter: Days 37-39: Using CSV data
Lecture: Demo: Parsing the CSV file

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We were able to read our file as text,
0:02 and that gets us really close,
0:04 but it doesn't really let us work with it.
0:05 And now, technically, we could do,
0:07 like a crazy bunch of string operations and parse this.
0:10 But, it turns out it is entirely unnecessary.
0:13 So we can import the csv module.
0:15 This is a built in in Python.
0:17 I'll call this reader, and I'll say csv.DictReader.
0:21 There's a couple of kind if readers.
0:23 DictReader is the best one.
0:25 And you pass it this file stream.
0:27 And what this thing is, is it is something that
0:29 you can loop over, and every time you loop over it,
0:33 you get the values out, okay.
0:35 So, let's do just something really, really quick, and,
0:39 just for row in reader, and just so
0:42 you can see it working I'll print out, let's say.
0:47 So if we run this,
0:48 looks like it's working,
0:49 and what we get is something called
0:51 an Ordered Dictionary.
0:52 Doesn't matter that it's ordered,
0:53 all that matters to us I can ask it for
0:55 the date, or the actual mean temperatures.
0:57 Like, if just want to print out this part,
0:59 I can go, because this is a dictionary,
1:03 let's comment that out, I can just print here,
1:08 row.get the actual mean temperature.
1:13 Great, it looks like we already got a lot of data,
1:15 and we could work with it.
1:16 There's one super, super challenging problem, though.
1:20 If I really want to work with this data,
1:23 you very likely want to ask,
1:25 is this thing less than that other thing, right.
1:28 Is this temperature less than that?
1:29 So we can answer our questions.
1:31 What's the hottest day, what's the coldest day?
1:32 Things like this.
1:34 So, let's print out the type, right.
1:36 This is going to tell us the actual underlying data type here.
1:40 Strings, everything that comes back
1:42 from CSV files are strings.
1:43 Even if they look like dates, or they look like,
1:46 you know, numbers or something.
1:48 And so in order to actually work with these,
1:51 we're going to have to do something a little bit different.
1:55 So, we're going to actually write a function
1:57 that takes this sort of raw row that we have here,
2:02 and instead of just saying we're going to store the text,
2:07 we're actually going to convert the numbers to numbers,
2:09 the dates to dates, things like that.
2:10 So we can actually answer questions like,
2:12 is this number less than that number?
2:15 Alright, the string less than or greater than
2:17 is not the same thing
2:18 as numerical less than or greater than, right?
2:20 Like, 100 is less than 7, for example,
2:25 if those were strings, but obviously not as number.