Python Tutorial 14: Saving and Reading Data Files With Pickle

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is paul mcquarter from toptechboy.com and we're here today with lesson number 14 in our incredible new tutorial series where you're going to learn python or you're going to die trying what i'm going to need you to do is pour yourself a nice cup of coffee that is straight up black coffee no sugar no sweeteners none needed and i'm going to need you to fire up the most excellent visual studio code and while you're firing that up as always i want to give a shout out to you guys who are helping me out over at patreon it is your encouragement and your support that keeps this great content coming you guys that are not helping out yet look down in the description there is a link over to my patreon account think about hopping on over there and hooking a brother up but enough of this shameless self-promotion let's jump in and let's talk about what we are going to learn today and that is before you get too much further along in python you are going to reach the day that you need to save your data to a file or you need to read data from a file so you need to be able to interact with your file system interact with your data sets and so that's what we're going to learn today now there are probably more efficient ways to do it there are probably faster ways to do it but what i want to show you is the simplest way to do it and the easiest way to have the most flexible data set in interaction with your data possible and what it is called is pickling so what we're going to learn today is how to pickle your data and how to unpickle your data so we're just going to jump right in here and i'm going to move out of your way and we are working in the most excellent python file program so i'm going to come over to python file folder i should have said and i'm going to click on the plus to create a new program and i'm going to call this my pickle now whatever you do don't call it pickle because if you call it pickle then it'll try to load the program itself instead of the library and so you need to call it something other than straight out pickle so i'm going to call it mypickle.pie i learned that the hard way that's how i wasted my afternoon was i named my program pickled up pie and then it confused python so mypickle.pie and then we get a fresh new python program just waiting to be written all right so now if we are going to pickle data and unpickle data we need to load the library now the good news is is that this library is installed when you install python so you don't have to install the library it should be there if you're on a more recent version of python and so you should just be able to do import pickle now this is importing a library and so what you got to understand is there are features that python can do that are not part of the standard installation because maybe they're not used that much and then those are libraries and to use them you have to import them in your program so we're going to say import pickle now if we're going to pickle data what i want to show you is i want to show you how flexible this method is i want to show you the flexibility so i'm going to create some data sets just some various data sets to just show you how easy and flexible the old pickle library is so let's create a string of ray we will call it fruits and fruits is going to have let's say apples that is a string apple and then the string oranges and then we're going to have the string bananas all right so that's a simple data set it is a string array of fruits now let's just say that x is equal to 7 so that is a x is a variable it is an integer integer value 7. let's say y is a float so we will make that 3.14 and now let's have nuts and nuts is going to have only two pieces of data and so that will be let's see what do you think how about pecans now notice that is pecan that is not pecan a pecan is a totally separate thing that we don't want to talk about the word for the nut is pecan okay and then we will say the most excellent almond okay so you see we have fruits is an array of three var of three values nuts is an array of two values x is just a plain old integer and y is a float and then just for good measure let's say grades and so grades are going to be numbers right not strings and so i'll say 99 100 56 and 77 and 85 like that so you see i've got one two three four five five data sets going from just a simple variable to different types of arrays all right so let's say that this is the data i'm wanting to i'm working with and i just want to dump it to the disk i want to dump it to a file well what i have to do is i have to open a file so i'm going to say with and this will actually create it i'm going to say with open and then what i'm going to open i need to give it the file name i'm going to call it my data all right mydata.pkl so that is going to create the file this is a file mydata.pkl this is something that will show up on the disk and then how do i want to open it i want to open it as right by write binary okay wb right binary and so that should open it and now i got to give it a name of how i'm going to interact with it so i'm going to say as f now you could call it anything but now f is just the simple little name that will point to my data so if i want to opera if i want to interact with my data i simply interact with and you couldn't see that as f and don't forget the colon okay don't forget the colon and darn it if i will remember to close these things you can see these things better okay so now with open i'm going to open my data.pkl as a write binary and it i will interact with it with a simple letter f so now i can start dumping data in that file so i just go pickle dot dump and what do i want to dump i want to dump fruits and where do i want to dump fruits into f and now i'm going to do a pickle dot dump and what do i want to dump i want to dump x into f now what i want you to see is i don't have to dump it in the same order that i created it i can dump it in any order that i want to but just however i dump it i've got to load it in the same in the same order so the first thing that i'm going to read out when i read it the first thing that's going to read in is fruits so let's put the rest of the stuff in there let's pickle dot dump and we are going to dump now y into f and now we are going to pickle dot dump and we are going to dump nuts into f does that look good pickle dot dump and we are going to dump grades into f okay so i created one two three four five chunks of data and then i'm dumping one two three four five chunks of data and so this should create that file now i'm going to tell you two things that are very important you need to save this okay because if you don't save it when you run it it's kind of i guess it's kind of ambiguous where the file is right this file might be somewhere in cash if you haven't saved it so you have to make sure that you save it and there it is mypickle.pie and it is in your it is in your python files folder now what it should do is it should put this mydata.pkl it should put it in that in that python files folder now if you don't save it it's kind of unclear if you don't save your program it's kind of unclear where it is going to put this data this data file all right if you really really want to be unambiguous what you can come in is you can come in and you can create your path here and then it will put it exactly where you tell it and that's probably the best thing is to put the path in and then it will put it exactly there but since we're on windows and you know how horrible the windows paths are i'm going to do it this way but if you have anything that isn't working perfectly the first thing that you want to do is you want to probably go in and put your path in but i think that this is going to work and so what i am going to do is i'm going to look over here okay and now when we run this it should create that file and again i just want to do this to make sure that we don't get an error when we run this okay we didn't get an error and boom do you see over here i got my mydata.pickle so that is just indicating that everything is working so we will proceed we're going to take a swig of coffee okay i've learned quite a few things the hard way one one thing i learned the hard way today is don't name your program pickle.pie that will then confuse it when you import pickle and the second thing i learned is make sure that you save your file before you run it and that way it will absolutely end up in the right spot the the data file will and then the third thing is if you still have any problems put your actual path in there all right so now what we're going to do is we're going to see if we can read it so i'm going to say with open and then what do i want to open i want to open my data dot pkl same thing if there's any confusion at all put the full path name in there and this time i want to open it as read binary rb all right so i'm going to open it as read binary and then we need our friend mr colon at the end and then again this will be indented and now what i will do is a i will read into the variable a a is equal to pickle.load and what do i want to load i want to load from f right did i forget that yeah i forgot that i need up here as f okay i'll just show you just for fun let's make it as f2 because we can make it whatever we want and then we just say a is pickle dot load f2 okay or we could make it anything we could make it f we could make it f2 whatever but whatever we open it as that points to mydata.pickle and then a equal pickle.load f2 it comes from there and then i think what we can do is probably just copy and then we're going to do paste paste paste paste because we had five pieces of data one two three four five that we put in there we're going to read one two three four five and this one then should be b and this one should be c and this one should be d and this one should be e or you can call it whatever you want it now i want you to see that i'm reading into different variable names than what i had to begin with and that's just because i want to make sure that i'm actually dumping to the disk and reading and if i use the same variable names maybe it didn't work and i just already had fruit set up right so i'm using a completely different variable name and what that will ensure is that will ensure that i am actually writing the data and reading it and so then what we could do to see if this is really working i could say just print a okay and then we're going to kind of go a little faster here and i think i can close that up we can do a control c and then ctrl v control v control v control control v and then we're going to print a we're going to print b we're going to print c we're going to print d we're going to print d and then we are going to print e all right and so we set the data up we save it and then we read it and then we print it and so let's see if this works d is not defined what is that nonsense ah i hope you guys were yelling at me up there i didn't read it in 2d okay now it should work hold your breath boom look at that look at that who's your huckleberry i'm your huckleberry look at that okay so let's look at this so what did it do it read all together it read the fruits it read seven it read 3.14 it read pecans almonds it read 9900 it read the grades and so everything that i wrote it read it in now you can see that you can kind of do anything that you want here like you could come and you could put fruits in twice right i could say pickle dot dump and then what could i do i could dump fruits fruits in again all right and then let's also just say i could pickle dot dump i could just dump i could just dump the number three all right i can just dump the number for three okay and then what i would need to do is i would need to read those two things f is equal to pickle.load and i'm going to load from f2 and then i could say g is equal to pickle.load and then i need to do f2 again and then down here i can print f i can print g and so now if we run this up here let me clear this out let me run it whoa i got a mistake here i got going too fast and where did i make a mistake it says line 14. oh i didn't say as f hopefully you guys saw that i need to dump that as f all right and then that hopefully will work and so we'll go here okay boom look at that so you see i have fruits now here and then i have fruits here again and then i just wrote the number three just simply wrote the number three directly and then that loaded into g and then it printed as three and so what you can see is is that you can dump any data types in there that you want okay i'm gonna do something crazy here what if what if we said something absolutely crazy here what if i said uh data set is equal to and what if i made it an array what if i made data set an array and what am i going to put in data sets i'm going to put fruits and that is a variable so i don't put a string around it and then i'm going to put x and then i'm going to put y and then i'm going to put nuts again that is a variable so no quotes around it and then grades okay i'm going to put all that in something called data set and then i'm going to open up f and then i am going to dump one thing and that is going to be i am going to dump one thing and that is going to be data set like that and then i don't need to dump the rest of this because i put everything in one array you know an array of arrays if you will and then what do i need to load i can just load it all into let me get rid of the others of these okay so now i'm just going to load one thing and let's call it uh the big kahuna i don't know how to spell ka kahuna the big kahuna all right and then what i'm going to print is the big kahuna like that and then i'll get rid of the rest of these all right and so now let's see what happens if i run this does not like that bug kahuna oh it was the big kahuna not the bug kahuna a completely different kahuna okay so now let's try this let's clear this and let's come over here boom look at that okay do you see it gave me a one big array and then that array had the first one the second third fourth and so i got one big array well you know what i could do now is instead of printing the big kahuna i could say for dt in big kahuna like that and then i could say print dt so it's going to step through the big kahuna and it's going to print the data one thing at a time like that right does that make sense so we're we've got this one huge data set that we created which was this data set which was fruits x y nuts and grades and then we pickled it all together okay we pickled it all together as data set and then we loaded it as the big kahuna and now we're stepping dt through the big kahuna and then it's going to print dt and so let's watch that all right and boom there it is it prints this array which was the first dt then 7 then 3.14 then this then this so i'm just trying to show you how flexible this is the only thing though is is that it you got to read it in the same order that you write it so if i write a b c d e when i open it to read it i can't just jump down and read e i've got to kind of read it in the same order that i wrote it but it's just a very flexible way to just dump data of any type to the file or to your disk and then read it back in boom okay guys this has been kind of a fun lesson i hope you've enjoyed this because it's just like really powerful to have an easy way that you can dump data to a file and then read it data from a file and so i think that what i need to do is i need to give you a homework assignment and your homework assignment is to write two programs okay the first program is going to ask you how many students you have and what that student's average is and so if you say like i got five students then it's going to say student name and student grade student name student grade student name student grade and so what it's going to do is you're going to say i have five students and it says inner student's name you'll say tom what's his grade you'll say 97 then enter student's name susan what's susan's grade you'll say 86 and so you will input the name grade name grade name grade name grade okay and then you will pickle that data all right then write a second program that will read the data and it says what student are you interested in and you say tom and then it tells you what tom's average is so you have to input the data and then you have to pickle it in the first program and then the second program it asks you who which student you're interested in and then it will tell you that student's grade so the answer will be tom has an average of 87. now you're not averaging grades you're entering the average grade so you'll say tom has an average of 87. susan has an average of 96 and then down here in the next program it says which end which student are you interested in and you'll say tom you'll say tom has an average of 97 okay so that's what you've got to do i've shown you everything that you need in order to do that but now you're going to have to really think and i'm going to do the solution next week because i i really want you to go and try to do this you know spend an hour so because you got to think through it right you got to think of your data set you got to think of your strategy you probably need to kind of map out on a piece of paper how you're going to do this and then you got to actually do it and then we'll be back next week and i will be showing you my solution and then what you need to do is if you get this leave a comment i am legend or leave a comment that you folded up yet again like a cheap lawn chair all right these things seem really easy when you watch me do them but really the first time you do them they're really really hard sometimes to get your brain around i don't think this one's going to be too hard i think you guys will actually be able to figure it out so you guys if you enjoy these lessons be sure to give us a thumbs up if you haven't subscribed to the channel hit subscribe make sure you ring that bell so that you will get notifications of future lessons and then as always i really appreciate it when you guys share this on your social social media paul mcquarter with top techboy.com i will talk to you guys later
Info
Channel: Paul McWhorter
Views: 8,202
Rating: 4.9840636 out of 5
Keywords:
Id: eWrTSBIQess
Channel Id: undefined
Length: 22min 56sec (1376 seconds)
Published: Wed May 26 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.