 And welcome everybody to Strings, Lists, and Dictionaries. In this video, I'm gonna show you how to convert from a .csv file to a dictionary. And in the process, learn about how strings work, how to split strings into lists, and how we can take lists and use those indices to create dictionary, key, and value pairs. So this is a bit more of an academic type thing. This is, I think it's important, and it's based on a lesson I did with my students today. So let's get started. But before I do that, I'm gonna say a quick shout-out to my members. Thanks to the paddles, the snakes, and the invaders. Your support is greatly appreciated. If you'd like to support the channel directly, click the Join button down below. Here we are. Strings, Lists, and Dictionaries. Let me go ahead and make that a little bit bigger to see if that's gonna work. And what I'm gonna be doing today is I'm gonna be taking a .csv file. I'm gonna be loading it into a string variable. I'm gonna separate that string variable into a list, and then I'm gonna take that list information and I'm gonna make it into a dictionary. Now, before somebody comments down below, yes, I know there are modules that will do this. We'll just automate some of this for you. What I wanna do with this particular lesson is, again, it's something that I taught my students today, is I want them to understand how all of these things are formed, how they work differently, and how we can convert from one to another. I think it's a valuable exercise. So let's get started. Now, the first thing we're gonna need is a .csv file. Now, a .csv file is what's known as a comma separated values file. So I've already gone ahead and added some information here. Now, I'm using LibreOfficeCalc, you can use whatever, but this should work as I expected, so hopefully this will go forward. Now, I could have just made this file in my text editor, Genie, but I decided to try this instead. So you can see here, I've got my first column, and these are names. So I've got Bob, Grant, and Greg, who are the three members of the legendary punk band Husker Do. And in the second column, I have their year of birth, 1960, 1961, and 1959. And the last column, I have their role in the band. So Bob Mould is the vocalist and guitarist. Grant Hart, who sadly passed away a few years ago, is also a vocalist and drummer, which is kind of unusual. And then Greg Norton is the bass player. So again, I want to save this as a .csv file. So watch what I do here. So I'm gonna go to File, I'm gonna go to Save As, and I need to save it into a particular folder. And so I made a folder on my desktop called Strings, Lists, and Dictionaries. So I'm gonna just call this data.csv. Doesn't really matter what I call it. I gotta know the name, but the .csv part is important. So I'm gonna click Save. And when you do this, it's gonna ask you, are you sure you wanna do that? And because the default format is called a ODF, Open Document Format. So I'm gonna go ahead and click Use Text CSV. And you're gonna get some more options here. I think the key one here is to delete this string delimiter. We want the fill delimiter to be a comma. Okay, because we could use other things. There's tabs, there's, yeah, et cetera, et cetera. But we want to use commas here for this particular example. Now I know that the data does not contain any commas, so we should be fine. So I'm gonna click OK. So now that file should have been saved. So I'm gonna go ahead and close that, and hopefully I won't need that again. And if I go into that folder, let's see it's on my desktop and Strings, Lists, and Dictionaries, you should see data.csv. And then this is the file here, the Python file I am working on. So yeah, so let's get started. So the first thing I need to do is I need to load the file into a string. Now again, I know that there are libraries to do some of these things. This is a step-by-step kind of from the bottom up. So what I'm gonna do is I'm gonna use the following code, and I got this actually off a stack overflow, I believe. So open, and it is data.csv. Again, that is the file name. And I am reading the file. I'm not writing to the file, I'm reading. So that's why I put R. And I'm gonna put this as my file, and I'm gonna change the font size a little bit because the underscores are not appearing. So my underscore file, and we're gonna put a colon here. And I'm gonna put here data, because I'm reading data in, and it's gonna be my file.read. Now there are other ways to do this. I can read it in line by line, but I'm doing it this way because it's a short file and it's a little bit easier, I think, to understand. So I'm gonna test it and see what's happening in my program. So I'm just gonna go ahead and print out data. So I'm gonna save that. I'm gonna run that, and again, I'm in Genie here, and Genie is cross-platform, check it out. And you'll see down below, it printed out exactly what was in my CSV file. So that's pretty cool. So the CSV format, as you can see, is it's gonna be text, comma, text, comma, text. And then we have a new line character at the end here, and we'll go down. So you can see, I'm gonna try this here, print type data. So if I run that again, you can see that this is a string. So I've just loaded the data into a string. Now, I'm gonna get rid of that, I don't need that anymore. What I wanna do, so I'll go ahead and comment that, so load data into string variable. So the next step is I want to create a list of each row. And this is very, very easy in Python, so watch what I do here. So I'm gonna be rows equals data. So this is the name of my string variable. And dot split, quote. And the split is used to separate strings. And this is called a delimiter, or delimiter, depending how you wanna say it. And in this case, it is gonna be forward slash N. At least I think it is. Some systems it could be forward slash R. But I'm pretty sure forward slash N is what we need here. And to test it, I'm just gonna go ahead and type print rows. So I'm gonna hit F5. And you can see here now, my output, I've got a list, you can tell by the brackets here. I've got a string, first string, which is index zero. Bob, 1960, vocals and guitar. Then I got Grant, et cetera, et cetera, and Greg. And you also notice I've got this empty one here at the end. And we're gonna have to deal with that later. But for now, we'll leave it the way it is. So I'm gonna say print type rows, and I'm just doing this to show you the type that we've got. And you can see that that is a list. So we have converted, so our code here, and I'm trying out some fancy new stuff here. So you can see our code, we take this data file here, and then we split it using the split method. And into this other variable, I don't know if this is helping or hurting. And then we've got our data in rows, as you can see down here. This is just something, today's like the first day I'm actually trying this. And I feel like John Madden, if you know who that is. So I'm gonna go ahead and get rid of that. And so now we have our data in rows. Now notice each row is now a string. Okay, so that tells us something. So what I wanna do is I wanna separate this information into three parts. So name, birth year, and I'll just say roll. So roll in the band. So now I wanna iterate through each row. So for row in rows. I'm just gonna go ahead and print row here real quick. If you're not familiar with this stuff, this is something I'm doing a lot with my students. So they would know what this is. But just in case, so you can see each row is printed out separately. Now what I wanna do again is I wanna separate each of these into their own data point. Now the way I'm gonna do this is split again. So I'm just basically gonna repeat what I did here. But it's gonna be a little bit different. So I'm gonna say, I'm gonna put a little comma here. Split, split the row into, I'll call info, make it clarify a little bit. So we'll say the info equals row. Notice it's row, not rows. So I'm doing each row individually. Dot, split. And in this case, the delimiter is the comma. Because the data has commas. It's comma separated values. So again, just to show you what's going on, I'm gonna go ahead and print info. And actually I'll type what I'll do. I'll do it one time. I'll go type info, comma info. So let's go ahead and run that. And you can see I've got a list and here's the list. So notice Bob, Grant and Greg are all in position zero or index zero, I should say. These are all in one. But also notice that these are strings. These are not integers. You know, to the human eye, they look like integers. To the computer eye, they are strings. We'll deal with that a little bit as well. And we've got vocals and guitar, vocals and drums in position of index two. Alrighty, so we are making progress. I'm pretty happy with this so far. Now, this is one of those things where I talk to my students. You know, I'm just gonna say I'm gonna separate out the data, I'm not sure how else to put that. But we've got three pieces of data. We've got the name, we've got the birth year. I think that's one word. And we've also got the role in the band. And again, if you look, this is index zero. This is index one and this is index two. So it'll be pretty straightforward. So I'm gonna go info, zero. You know, info one and info two. Now, just for, you know, kind of completeness here, note that this is a string, but I want it to be an integer. So I'm just gonna go ahead and convert that right now. So I'm gonna go ahead now and print out the information. I'm gonna use an F string. I'm gonna say name. Name. And I really strongly recommend that you use consistent names for your variables. It will save you lots and lots of struggle later. And roll. And again, if you're on an older version of Python, you might not have F strings. You just print this out the normal way. So now I'm gonna go ahead and run this. And there's gonna be an error. And I'll explain why in a minute. Oops, I'm gonna go ahead and try and run this. There we go. So notice it started printing out. Okay, so I got Bob, 1960, Vocals and Guitar. Grant, 61, Vocals and Drums. And Greg, 1959, Bass. And then I get an error. So if you recall, the CSV file, when we split it, it gave us that extra little, okay, it gave us that extra little, you know, that row at the end. And it was empty. But even though it's empty, we still have to deal with it. Now, one of my students suggested that we just delete that row. And that's not a bad idea. We could have done that. We could have done, we could have just, I think we could have just done rows. Basically we could have just done rows equals rows like that. And what would it be? I think just, yeah, yeah, like, like, it'd be like zero length rows minus one. There's probably a shorter way to do that. But we could do that. We could cut out the information that we don't want. However, you know, it is possible that we would get data that has maybe the first row is also blank or a row in the middle is blank. So another way to do this is to look at this data. And notice when we split the row, it's got three pieces of information. So I'm gonna use that to check. So I'm gonna say check if the row is the proper length. Now, if it's in the wrong order, we're out of luck. But that's what we're gonna use. So I'm gonna put that up here. So I'm gonna say if the length, oops, I'm gonna put that back in. So if the length of info, we should have three items for this particular data. If it has three pieces of information, that will help us avoid that error. Again, I know there are other ways to do this. This is what is appropriate to the level of my students right now. So this is why I'm doing it this way. So I'm gonna go ahead and run that. And you can see now no error. And it just ignored that information because it didn't have three pieces of information, which is pretty cool. Okay, so we're making really good progress. At this point, I guess we could stop. But what I wanna do is I wanna go one more step and I wanna put all this into a dictionary. So, and this is one of the things where, again, especially for beginners, you gotta think about where things need to be done in a program and especially when you're iterating, make sure things go where they need to go. Sorry, I'm drinking a little tea here. The air is very dry. So what I'm gonna do is I'm gonna go ahead and I'm gonna create a dictionary. And what can I call this? I guess I can call this members. Members, because band members. I guess that's how I wanna just call Hooskidoo. What the heck? Hooskidoo equals. And now again, you would probably make this a little bit more generic in another program. But again, these are the members of Hooskidoo, so I'll just go ahead and use that. So what I wanna do is I want to use the name as the key. Again, I'm assuming you already know the dictionaries. If you don't watch my dictionary video, come back. So what you wanna do is I wanna use the name. See, I can bring this back. I can use the name as my key. And notice I've got two pieces of information here. That doesn't help. I really gotta work on this. I've got two pieces of information here that I could use as the values. Now, what I'm gonna do is I'm gonna do just one piece of information, and then I will do the other piece of information. I'll show you how that's gonna work. So I'm gonna kinda build this up piece by piece just to make it hopefully comprehensible. So what I wanna do here is instead of printing, so I'm just gonna comment that out, I'm gonna put populate the dictionary. So what I'm gonna say is I'm gonna say my dictionary is who's gonna do. The key is the name of the band member, and the value is gonna be the birth year. And then what I'll do is once this dictionary, or once the dictionary's populated, once all the rows have been read, I'm gonna go ahead and print the dictionary. So let's try that. All right, there we go. So you see now my key is Bob, and the birth year is 1960. Grant, 1961, Greg in 1959. Now, however, you probably see, well, okay, the roll info is gone. Okay, we've lost that. Now, what we could do is we could make a list, who's gonna do birth year, or make a dictionary, who's gonna do birth year, or make another dictionary, who's gonna do rolls, okay? Or maybe I should just, I probably should just make this band, but that's okay. You get the idea. So here's where it gets a little tricky, especially for beginners. And what we're gonna do is we're gonna actually put a dictionary inside of the dictionary. Okay, let me repeat that. We're gonna put a dictionary inside of a dictionary. And in the second dictionary, the key is gonna be birth year, and the other key is gonna be roll. Okay, so watch what I do here. So again, the outer dictionary, the main dictionary, who's gonna do, the key is the name. I'm gonna make another dictionary. The first key is birth year, and I'm just gonna put birth year. The second key is gonna be roll, and I'm gonna put roll here. And so if I run this, you can see now I've got Bob, and I got his birth year is 1960, his roll is vocals and guitar. I've got grant, okay, and so on and so forth. I'm gonna keep repeating myself before I get tired of hearing it. But you notice there is a dictionary inside of the dictionary. So what I could do here is I could say for name in this overdue, because the name is the key. I could just put key here if I wanted to. Or for name in his overdue, I'll just use name. Print name, sorry, no, print whose overdue, do name. And again, I could use single quotes here. I think it's actually the preferred way of doing it. I've just never gotten used to it, and I apologize to everybody who hates that. So I'm gonna run it. Key error name, that was not very bright. Okay, so I should use the variable, duh, okay. So you can see now, what I should have done is I should put name comma, let's go ahead and do that. So you can see now I got Bob, and then that's a dictionary now. So this is one dictionary, but I still wanna get birth year and roll out of this. So what I'm gonna do is I'm gonna do the following. I'm gonna put birth year and comma. I'm just gonna copy that because I'm kind of lazy. And this is gonna be a roll. Let's go ahead and try that. Okay, so you can see now I've got a dictionary and I pulled out, okay, the name is the key for his overdue, the dictionary whose overdue. Then the secondary dictionary that's inside of that has this as one piece of data that the key to that is birth year and the key to this is the roll. So yeah, I could add more dictionaries if I wanted to, it gets a little complicated at that point, but two level dictionary isn't too, too difficult I think to comprehend. But basically, like I said, this is the key, this is the dictionary, this is the key. So it's dictionary, first key. So in this case, that would be Bob Grant and Greg. Second key is birth year and the same thing over here. So that is pretty much that. That's basically what this whole video was about. So again, I read all of the CSV data into this file and actually into this string. Then I split it into rows using the new line character. I created a dictionary. Then I iterated through each row and then I split the information in the row into where there's a comma, I use the comma as the delimiter. And then I just wanted to make sure the row is the proper length, so I'm assuming I've got a name, birth year, and roll in that order. And then again, notice how I'm using very clear variable names. Now if you're a really good programmer, you're doing this a long time, I could just take this out and put this here. And then I could take this out and put this here. And I could take this out and put this here. And I could just delete this out. And then this will do the same thing. But let me ask you this, what's more readable? Now right now, because I'm doing the program, I completely understand what's going on. But if I come back to this a year from now or probably even a week from now, I'll be like, I don't know what's going on. So I find it really, really helpful to separate things out in a step-by-step fashion. It takes a little more memory, probably takes a little more time processing-wise. But I think in terms of code readability and updateability, I think it'll benefit. And then I just iterated through the keys, printed the key, the key and birth year combination, and the key and roll combination. And that's, my friends, is that. So thanks for watching, I do appreciate it. Again, thanks to my members, thanks to everybody who has subscribed, thanks to everybody who's joined and commented. And especially those thumbs up. So click down below and, as I like to say, keep on coding, take care.