Loading...
Hi, I'm Rebecca, your computing teacher for the programming part six unit.
For this lesson, you're going to need your which you should have already set up with your parents or carers permission.
You're also going to need a pen and paper to make any notes and answer any of the questions that I give you in this lesson.
It's a really good idea as well to switch off any mobile devices or anything like that.
Get those notifications stopped, remove as many distractions as you 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 write data from a one dimensional list to a CSV file, and you'll write data from a two dimensional list to a CSV file.
Think about this then, when might you choose to use a CSV file instead of a standard text file.
Pause the video while you have a think So you might've come up with things like this, if the data needed to be read in a spreadsheet software, I would use CSV.
I would use a CSV file if the data needed to be in tabular form and if the structure of the data needed to be in a specific format, I would use a CSV.
So you've probably come up with some examples similar to some of those situations there.
We're going to be looking at writing to a CSV file now.
So writing to a CSV file is very similar to writing to a standard text file.
The key thing is to remember how a CSV file is structured.
So each item in a row is separated by a comma and each line represents a new row or record.
The next thing to remember is that a CSV file is a text file.
You can only write data to it that has the string data type.
He is a programme that is attempting to write an integer to a CSV file.
When executed this error message will occur.
A list data structure is also not string.
You can't simply send a list data structure to a CSV file.
A similar error message will occur.
Any data written to a CSV file must be sent in the format that it requires.
So data must have the string data type, each item should be separated by a comma and each new row or record should be separated by a line space or back slash, and which stands for new line.
So what will be stored in a numbers.
CSV file when this programme is executed.
Pause the video while you have a think.
So this is what it would look like.
And here's the view from a text editor, and here's a view from a spreadsheet software as well.
If you have a list of items that need to be written to a CSV file then you need to make sure that the data is held in the correct way.
So what will you need to do with this list to make sure they can be written to a CSV file do you think? Pause the video while you have a think.
So here's the answer, you're going to have to change each item from an integer to string 'cause at the moment they're all integers you can't see any quotes around them so they must be integers, add each item to a single string and then add in the comma between each item.
To change a datatype of each item you can append to a new list whilst casting each item as a string.
That's what that bit of code does.
So create a new list called str numbers for every number or item in the numbers list, append the new list string numbers with the returned string number.
This will give you a new list based on the original list where each item is held with a string data type instead of that integer data type.
So here is now the state of string numbers.
Once you have a list containing items of the string datatype you need to turn the whole list into a single string.
Can you remember what the split method does? Can you remember that from last lesson? Pause the video while you have a think about that.
So the string method converts a string into a list.
The string is split where a specified character is found.
This is the join method.
It is also a list method.
It does the exact opposite of the split method.
The join method takes a list and return the string.
Items are joined with a specified character.
So let's take a look.
Join is a list method, split is a string method.
Note how the syntax is written differently.
The words list holds a list of words.
The join method takes the words list and returns a string.
A hyphen is placed between each word.
The character specified is used to link the items together.
This can be left empty to join items directly next to each other.
So you can see the difference there.
Keeping that in mind then, how can the join method help with this programme? Remember that we want the content of the list to be written to the CSV file.
So if you can use join to create a string of items that are connected with comma.
By joining the list into one string, you have a string that is ready to be written to a CSV file.
So you look there, you can see the difference now between that data.
You now got a perfect bit of data that can be written to a CSV file.
You can use exactly the same code that you use when you write to a standard text file at this point.
And this creates a new CSV file with the new data stored inside.
And there's the view from a text editor and the view from a spreadsheet.
So that seemed like a lot of steps, but actually it's only a few steps.
It's just getting you to understand how those steps actually work.
'Cause it's quite a lot going on, but it's not actually that many lines of code.
So what I want you to do is use the right to a CSV file section of your worksheet to try some challenges that involve lists and CSV files.
And then you'll be able to see how that code actually works when it's in person.
And hopefully you'll be able to understand how you can then use that code for other situations, just say same snippets of code for other situations, 'cause this is now what we need to start doing.
We need to start looking at code that we've used in the past, or looking at those code snippets and deciding how they can apply to our current problem that we're solving.
So pause the video while you have a go with that.
So here is the solution to challenge one, pause the video while you have a look there and compare that to what you did.
And then ere is the solution to challenge two, so just pause the video while you take a look.
Compare that to what you did as well.
So now we're going to look at writing a 2D list to a CSV file.
So you've already written a single list to a CSV file.
This required converting the data to a single string with the addition of commas as separators.
What further step is needed for a 2D list do you think? A new line space or back slashing at the end of the row or list.
So if you look there, you need to make sure you've got that new line so that it knows to go to the next line in the CSV file.
Using the same code from the first task on a 2D list will produce this CSV file.
That's not going to be what you want, is it? A 2D list is a list of lists.
This programme is converting each list within the main list to a string.
It might look like it might be right, but it's not, is it's going to look a mess.
So you can see that the H list is just being converted to a string In order to process a 2D list so that it can be written to a CSV file.
you need to access each item within each list.
You also need to know which row or list the item belongs to.
Just like before you need to convert each item to a string, and you now have the added task of keeping the 2D list structure.
So having that backslash in there as well.
You still need to populate a new list with the string values.
So definitely that start code.
This list must now be a 2D list instead of a 1D list, and this requires some extra steps.
For each item or row in the number grid, you need to create a new row, access the item in the original row and return them as a string to the new row.
Append that row or list to the str number grid.
And this is how it would work.
Here's our original number grid with all of those numbers there in a 2D list and you can see as well that they are all integers because there are no quotations around the numbers.
Then we initialised string number grid to get it started so that we can append to it later on.
Then we have a four item in grid.
So we're going to be iterating through the items in a number grid.
And remember each item is a list because this is a 2D list.
So each item is it's own list 'cause it's lists within lists.
So we start off by looking at that first one, we're going to create initialise a new list called row with nothing in it at this point in time.
And then we're going to iterate through that first item in number grids.
So if you look there, we've now got item is holding one, two, three, and we're going to eat a rate through item.
So that's the first one and we'll hold that value, we'll append it to row.
Then it will go to the next item and it will append it to row.
Then it'll go to the next item and it appended to row.
Can you see that? So what happens next? Do you think? So at this point, that row now gets appended to our str number grid list.
And now we work our way through to the second row or the second list in number grid, our original one with the integers.
So now row gets initialised again to a blank on there always values And then we go through each item.
So four gets changed to string and added to the row, five gets changed to string and into the row, six gets changed to string and added to row.
And now what happens next? Row then gets added to str number grid.
So now we've got 2D list on the go because we've got two lists within that list and they are all the string.
And then we've just got a last bit to go so now we're looking at the last item in our original list and we're going to go through again.
So note row is initialised again, and we go to seven, convert that to string, eight, converted to string, nine, converted to string.
And now we've got a new list with all those same values, but they're now strings.
So what happens next? It then appends to that one.
So now we've got number grid, our original number grid with integers in and now we've got string number grid with string values.
So is not the same values but now they're string rather than integers.
So you now have a two D list containing string values.
This is still not ready though, to be written to a CSV file.
So as I said before, quite a short of code but there's a lot going on there for you to kind of get your wrap your head around 'cause it can be a little bit confusing when you look at it the first time.
But once you've got that snippet of code and it's working, you can just apply it to all situations, every time you need to take 2D less and put it into a CSV file, you can just use this code snippet again.
And it'll just do it for.
Maybe just a little bit of modification, but not too much.
So what steps do you still need to do to prepare it for the CSV file then? Can you think about that? Pause the video.
You need to join the 2D list into a single string now.
If you remember, you cannot send a list to a CSV file.
It's not going to work.
So a text file, sorry, it's not going to work, it's expecting strings.
You going to make sure it is a string.
Now the joint method will not work on a 2D list.
This programme will cause an error.
The join method looks for a single item in a list to join as a string.
Instead of a single item, it finds a list and this displays the error.
You can use the join method for each item in the list or list in the 2D list.
So a fall loop committees to iterate through each item in the 2D list and join the string together.
So instead of doing it on the whole 2D list, all in one go which is going to produce an error, we can take each list in that 2D list structure and do it one at a time.
So that's what's happening here.
So why is the back slash needed here? Can you remember? So this is to add a new row after each record 'cause remember it's going to have that whole row and then it's going to need to go to the next row.
Otherwise it's just going to do one massive loads of columns all the way across which we don't want.
The final step is to write the string data to the CSV file just like you have done before.
So those same snippets of code.
Once you know how to do those little snippets of code, you can just apply them to any situation where you got to write to a text form.
So what we'll need to do now is use the write a 2D list to a CSV file section of your worksheet to perform challenges that involve 2D lists and CSV files.
So pause the video while you have a go at that.
Excellent, so here is a solution for challenge one, is quite long this one.
Again, I've separated it into two half.
So the left part is the top and the right part is the bottom it is a warm programme.
So you can check that with your code as well.
So pause the video while you do that.
And then this is challenge two solution.
This is part one, so I'm going to show you the other part in a minute.
So pause the video while you have a look at that.
And then this is part two.
So you can pause the video while you look at that.
You might want to keep skipping back so that you can see the whole thing together.
Let's try and remember what some of those methods do.
Can you write down a description of what those methods do? Pause the video while you do that.
So split splits a string into a list, join joins the items from a list into a string and append adds to an existing list.
So well done, you've now written two CSV files using a 2D list.
So that is quite a complex thing, but hopefully you've figured out now, which bits of code you need, how they work, and then hopefully you can then apply those bits of code to other scenarios that you might be given any but you are going to need that for the big project that is coming up in this unit.
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 'cause it's always good to see what you've been up to.
And I'll see you again soon for next lesson.