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

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Our goal is to read that CSV file.
0:03 We technically could do that right here,
0:05 but I would like one part of my application to just be
0:08 dedicated to reading, and parsing,
0:10 and working with the data.
0:11 And another to do with this, kind of, UI business.
0:13 So let's make another Python file called research,
0:17 and we'll put that at the top of level of our program here.
0:20 Now we're going to want to work with the CSV file.
0:23 So let's come over here and define a function called init.
0:27 And this is going to basically load up the data,
0:30 the data file and then get it into memory
0:34 and we can answer questions about it later.
0:36 We're going to have this global shared piece of data like this,
0:40 and we're just going to make that a list.
0:44 So we're going to initialize this, and now what we need to do
0:46 is we need to determine this file.
0:48 It would be very tempting to say file
0:50 name is just
0:52 ./data/seattle.csv,
0:57 or back slashes if you're on windows.
1:00 Now, be careful, backslashes and strings
1:02 don't mean quite what you think they mean.
1:04 This, if you said something like \t,
1:06 notice how it changed colors, or \n.
1:08 Backslash and strings is used as a,
1:11 what's called an escape character,
1:13 and it means \n actually stands for a return,
1:16 and \t for a tab and so on.
1:18 You can use double backslashes to say,
1:21 no no, I just mean the backslash.
1:22 All right, so be a little careful there.
1:24 But, even so, that withstanding,
1:27 this, and the spelling as well,
1:30 this is not going to be working out really well.
1:32 It'll work if we'd run it actually,
1:35 but only if the working directory is this folder.
1:38 If you run it from the command prompt,
1:40 or terminal, and you're somewhere else,
1:42 this isn't going to work.
1:43 So we're going to take a slightly more complicated step
1:46 to determine this location.
1:49 So we're going to come up here and import the os module,
1:52 which let's us ask cool questions.
1:54 And we'll say folder, let's call it basefolder,
1:59 = os.path.dirname of where.
2:04 Well, one of the things we have to work with that's
2:07 in variant is where this file is located.
2:10 And what we want to do is go to where this file is
2:12 located, go into the data folder, and go here.
2:14 And there's a way to always get to that in any Python
2:16 file just say dunder file like this.
2:18 So, this is a file,
2:20 dir name, we'll drop the file part,
2:22 and just keep the directory,
2:23 and then we need to say filename, here is
2:26 actually going to be os.path.join,
2:30 and we want to put together the basefolder and data,
2:33 no slashes, and seattle.csv, okay?
2:40 So this will do that in a platform independent,
2:44 location independent way.
2:45 And it's really, you know, not that much work
2:48 above what we were maybe going to type in the first place.
2:50 So let's just open this, we'll use a with block,
2:54 we'll say open file name, and we can give it a read,
2:56 and one can even say encoding=UTF-8,
3:03 that's the safest guess, and we'll say as fin,
3:05 that'll name our variable.
3:06 Let's just do a quick print,
3:07 forget CSV for a minute, we just want to see what's in here.
3:10 So we can say, read lines, or just read,
3:14 look at all the text.
3:15 So now, let's call this function,
3:17 it's not done, it's not even close, it doesn't do
3:20 anything with the data, it just pulls it in as text,
3:22 but it's going to verify this one aspect of what we've done,
3:24 reading the file.
3:26 So let's go over here, and let's, before we do any of this,
3:30 let's go do our initialization.
3:32 So we want to go to the very top, say import research,
3:36 all right, and then down here we can say research.
3:39 forget data, we're not going to work with that directly,
3:41 but we'll say init.
3:42 Now, let's run this and see what we get.
3:45 Boom, we saw a whole bunch
3:48 of CSV data scream by, just like that.
3:51 How cool is this?
3:52 So this is totally working well.
3:54 We've got our research, it can find the file,
3:57 and I can tell you it will find it from
3:59 anywhere on the computer.
4:01 It doesn't matter, it's going to just use the location
4:04 relative from here to there, based
4:06 on this thing that we did.
4:07 All right, so that's how we load the file up
4:10 and get a file stream, that's one half of the problem.
4:13 The other half is to take that giant string
4:15 and convert it into data we can work with.
4:18 That's the next step.