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


Talk Python's Mastodon Michael Kennedy's Mastodon