Python Part 16b - Linking to CSV files Office

Python Part 16b - Linking to CSV files Office Welcome to this wisel tutorial on linking to csv files in python here's what you'll learn during the tutorial so we'll begin with a quick warning.

About pandas just to check you're watching the right tutorial and then we're going to look at how you can write csv files and how you can read them back in again csv stands for comma.

Separated values by the way we'll then look at how you can use something called a dict reader in a csv module and what that allows you to do is to reach each read each forever file.

Into its own separate dictionary and finally i'll look at what i think is a more useful technique which is reading a csv file normally and reading adding each row into a dictionary that you've.

Created at the top right of the screen you'll see a link appearing about now and you'll be able to click on that to see any files or exercises to do with this.

Tutorial but as always i'm going to vanish now and sven will take you through the rest of the proceedings over to sven.

Python Part 16b - Linking to CSV files

So let's get started i'd like to begin this tutorial with a very quick warning about pandas not those pandas pandas the module and python and it's this but if you're.

Importing a table of csv data into a pandas data frame there's a much easier way to do it in pandas than what i'm about to show you in this tutorial but if all you know about pandas is.

They're black and white cuddly bears from china then please watch on so for the examples for this tutorial we're going to look at reading and writing out the names of our wretched.

Cats i'm afraid so it's time to meet them again they are if you remember annie who's a torto shell cat and her brother neo who's black and white he looks mostly black there but.

There are quite a few white bits as well so in this tutorial what we're going to do is write out and read in a list of our cats there's going to be three columns the cat name which will be what.

The cat's called the color and when the date of birth is i'm going to assume the cats were all born on 15th of august 2019. they were certainly born in the same day but it was sometime in august i.

    Forget exactly when

    But i feel that's not vital for this tutorial so what we're going to do is create a new file called writing csv files dot py.

    And the first thing i'm going to do is import the csv module this is a built-in module so there's nothing to install and it allows us easily to work with reading and writing.

    From and to or to and from csv files so i'm going to open up a file for writing and you can do that in the standard way by saying with.

    Open and then i'll put a little r in so i don't have to escape my backslash characters and then put the specification of where the file is.

    And i need to include a second argument which is w otherwise i'll be reading it so w means write if you remember a means append i want to overwrite it each time.

    I created so i've used w and i'll call that cat file then i've got that open what i want to do is create something called a csv writer.

    And that will allow me to write out easily to a csv file and to do that i'll create a new variable called writer and i will.

    Set it equal to a csv writer based on the file i've just created and then i can start writing out my information so firstly i'll write out.

    The header and to do that i can say writer dot right row and what i need to do is provide it with a natural iterable object.

    So what i'm going to do is pass in a

    List of things the first thing will be the heading cat name and i'll have the heading.

    Color and then i'll have date of birth after writing that out of the file i can now write the actual cats.

    That again like about writer.right row and i can write out the first cat who is called annie i'll put that in quotation marks just to show you shouldn't be too afraid of.

    Those those she's taught her shell is born allegedly on 2019-08-15 what i needed to do was to include that in square brackets to make it an.

    Iterable object otherwise it won't be accepted by the right row command and what i'll now do is just amend that slightly to get near and near is black.

    And white i was born on the same date say it's finished and just to prove this has finished my writing i'll print out a message saying that's.

    It what i can try doing now is to run this program so if i press all control n as always i get the message that's it and i get a.

    List of cats now you can see that it's overwritten the file because i've got a different version i've got blank row alternate blank rows and the reason i got this is.

    I didn't add an additional argument when i was creating the file which is to say that when i create a new line i should use a blank string for a new line character.

    So if i run that again now and again have a look at my cats.text it's refreshed automatically which is quite clever and it gives me the list of cats.

    So that's how you can write a file in csv i guess one question i would ask is is there really so much benefit in doing that ever just using a standard text writer i can't actually really see the.

    Difference between the two at all and it's only really when you start using dictionaries as we will later in this tutorial that you'll probably derive much benefit.

    From using the csv module so having written out a list of cats let's read them back in again so we're going to look at reading csv files and i've.

    Created a program for that effect so what we can begin by doing is importing the csv module and then what i want to do is open the text file for reading this time.

    So to do that i can say with open and then in quotation marks i can add in the path to the file i need an r before it to avoid escaping the characters.

    And i'll call that cat file again why waste a good variable name so what i can now do is create a csv csv.

    Based on this i'll call my variable reader i don't have to that's just a good variable name so i'll take my csv module and apply a reader to that.

    And what this will do is make it slightly easier to read in the csv data than if it was just a standard text file but as before it really is only slightly easier.

    So what i'm now going to do is to read in the header a csv file is always assumed to have a header row.

    So to do that i'll create a variable called header and read in the next line from the reader that's how you do that and just to prove this has worked let's print out the header.

    And because i'm a great believer in testing things as i'm writing them if we run that program you'll see i get my header containing a list of the three headings.

    So what i can now do is i'm just going to come in the tower because i don't want that anymore what i'm now going to do is to read in the rest of the lines so i'm going to.

    Say four row in reader so it's just like for a normal file this will automatically go over the rows one at a time.

    The only difference is the row is defined as a csv row and will automatically contain a list of all the items within it.

    So just to show what it's going to do i'll just print out the row without any modifications at all for the moment and if we run that you'll see it gives me the list of the cat name color and.

    Date of birth to extract those bits of information what i'm going to do is unpack that each of those lists so i'll set three variables called cat.

    Name color and dob and set them equal to the row so now if i print out.

    Those bits of information so and run the program then you should see that i'm getting the three bits of information separately and i could stop there but i'm conscious.

    Many people work with dates in csv files so i just thought i'd show you how you can convert this date of birth into a proper date so we can format it so to do this you'll almost certainly.

    DISCLAIMER: In this description contains affiliate links, which means that if you click on one of the product links, I'll receive a small commission. This helps support the channel and allows us to continue to make videos like this. All Content Responsibility lies with the Channel Producer. For Download, see The Author's channel. The content of this Post was transcribed from the Channel: https://www.youtube.com/watch?v=LXBMWoOKOic
Previous Post Next Post