video

Lesson video

In progress...

Loading...

Hi I'm Rebecca you're computing teacher for the programming part six unit.

For this lesson, you're going to need your [indistinct] account which you should have already set up with your parent or carer permission.

You're also going to need some pen and paper so that you can make some notes and answer any questions.

And it's a really good idea to try and remove as many distractions as you possibly can so that you can really focus in this lesson.

Once you've got all of that ready we can begin.

In this lesson, you will describe a CSV file.

You'll read from a CSV file, you'll give a split method and you'll select data from a collection of values.

Let's start off then by making a prediction take a look at this code, it's code that you will have seen you'll be familiar with from a previous lesson or unit may be.

And I want you to make a prediction about what the output of print will be when this programme is executed.

So pause the video, while you read that through and try and figure it out.

Excellent let's take a look then.

So it would have printed play a name DinoFish and SharkDragon.

So let's look at CSV files now then.

CSV stands for Comma-Separated Values.

CSV files can be read by a range of different spreadsheets software packages, as well as Python files.

CSV files contain data structured so that a comma separated individual items in the file and each record is on a new line so you can see that there.

So you've got there an individual item and you've got there the comma separated items. And each line is on a new record.

A CSV file can be created in a text editor by changing the extension of the file to CSV.

So if you were doing this in you're, on your computer without Repl.

it then you could open up a package like notepad and then just save the text file as a.

CSV file rather than.

TXT.

And you can do the same thing in Repl.

it where you create a new file instead of putting.

TXT there you can put.

CSV and it'll do the same thing.

So you can also create CSV files in spreadsheet software and export them as a.

CSV file.

When you open a CSV file in spreadsheet software the commas are used as a guide to display the data neatly in rows and columns.

When a CSV file is opened in spreadsheet software it is displayed in tabular format, so you can see that there.

A CSV file is still a text file.

It's important to be aware of that each row is stored as string and that is very important.

Especially when you're thinking about the spreadsheets and you think oh there must be numbers so that you can do calculations and formulas and things like that on them.

They are still going to come through as string.

So modern spreadsheet software will typically recognise a number and storage as that data type 'cause they are pretty intelligent.

The Python will not do this automatically.

Any data is always read as string and will need to be casted before it used as any other data type.

So spreadsheet software can do that for you because it's used to pulling in CSV files, whereas in Python you've got to add those extra instructions in for you.

So when do you think you might use a CSV file with your Python programmes? Pause the video while you have a think.

So these are some possible answers that you might've come up with, so scores for a game, a class register or usernames and passwords maybe.

A CSV file is just another text file.

So you can use all of the same methods that you already know with a CSV file.

Using the read method we'll copy all of the data into the Python programme in the same way it did with your text files.

So there we go.

The modes are also the same so you can use, read, write append and read and write.

The difference for the CSV file is that you might want to perform different operations with it in comparison to a standard text file.

This means that you need to think carefully about how your data might be structured as it is read into your Python programme.

Data represented in CSV files is often in a tabular form.

This means that it is stored in rows and columns.

So here's a question for you, in order to manipulate data that is stored in rows and columns, what data structure might you use have a think? So the answer would be a 2D list or a list of records.

Reading data from a CSV file and holding it in a list is very similar to the method you use for standard text files.

Here is a CSV file with one column of data.

When you assign the contents of the file to a variable, it is held like this.

So you can see there.

Just as with a standard text file, the backslash N is included.

To assign this data to a list data structure you need to append each line to list.

So what will be held in the data list when this programme is executed do you think? Let's take a look.

So that is what will be held and you can see it is now in a list is separated, each item is separated by a comma.

So each item is now in a list but you still have the extra piece of data to deal with so that backslash N part.

What method can you use to remove unwanted pieces of data can you remember? So you can use the string method.

For each line in the file you need to strip the back slash N.

So note the strip method defaults to removing any white space from the start and end of a string unless you specify the characters as arguments.

So you now have a list with all of the extra pieces of data removed.

That's looking a lot better, isn't it? You can now interrogate this data as you would with any other list.

So what I want you to do is use all of that, that I've just gone through with you and use the read from a CSV file section of your worksheet to practise reading from a CSV file.

Have a go pause the video while you do that.

Excellent, right, let's look at the solutions then.

So the challenge one solution, I've just split it over two sections.

So the left part is the top and the right part is the bottom.

They do follow on from each other, I just had to display that so you could see it.

So pause the video while you just check your work.

And then this was challenge two solutions.

So pause the video while you check your work.

Brilliant, right so now we're going to look at reading a CSV file to a 2D list.

So CSV files present data in tabular form.

The last example used only one column of data.

You are now going to work with a CSV file that has two columns of data.

The players.

csv file has stored has data stored in two columns and three rows.

If you use the same code as the previous activity with this CSV file then the data is held in a single list.

So you can see it happen in there.

For each row in the CSV file, each row in the CSV file is held as an item in the list.

So you can see that happening there.

This means that each item has two pieces of data held within it, which is impractical.

And note that the commas from the CSV file are part of the string here and are not separating items in the list.

Can be a little bit tricky to you might sort of confuse that a little bit when you see a comma there.

For each piece of data to be held as an item, the data needs to be split at the comma in each line.

The split method takes a string and splits it into a list of items. So this is quite a useful method in this situation.

So the word variable holds a string of words.

So we can see there an example, here are a lot of words.

The split method splits a string where it finds a space and returns it in a list and you can see that example there.

The character that is passed as an argument into the method will be used to split the string.

Changing the space to an O will result in a different list.

So you can see what's happened there.

Can you see, can you see what's happened that difference? So know that the letter O is also removed from the string and it's quite important to remember that one too.

So how might the split method help with our CSV file problem that we've got? Well, it can split each line when it finds a comma.

While splitting each line into a list you now have a 2D list and you can see that there.

This will now allow you to perform operations on the data.

So what I want you to do now is use what you've picked up there to have a go at practising.

So use them use the read from a CSV file to a 2D list section of your worksheet to practise reading from a CSV file to a 2D, 2D list.

So pause the video while you have a go at that.

Excellent so here is the solution to reading a CSV file to a 2D list so pause the video, while you check that through with your own work.

And here is that other one, the second one.

So pause the video while you check that with your work.

Excellent so you've now seen how CSV files can work with Python too.

And if you'd like to please ask your parent or carer to share your work on Instagram, Facebook or Twitter, tagging @OakNational and #LearnwithOak.

It's great to see what you've been getting up to.

And I'll see you again soon for our next lesson.