Loading...
Hello, my name is Mrs. Holborow, and welcome to Computing.
I'm so pleased you've decided to join me for the lesson today.
In today's lesson, we'll be learning how to write data from both one-dimensional and two-dimensional lists to a CSV file.
Welcome to today's lesson from the unit Programming dictionaries and data files.
This lesson is called Writing to CSV files, and by the end of today's lesson, you'll be able to write data to a new file and append data to an existing text file.
Shall we make a start? We will be exploring these keywords in today's lesson.
Should we take a look at them? List.
List.
A dynamic data structure that can contain items of different data types.
String.
String.
A sequence of characters, e.
g.
letters, numbers, spaces, and other symbols.
Join.
Join.
The join method takes a list and joins each value in that list to one string.
Look out for these key words throughout today's lesson.
Today's lesson is split into two parts.
We'll start by writing data from a 1D, or one-dimensional list, to a CSV file and then we'll move on to write data from a 2D list to a CSV file.
Let's make a start by writing data from a one-dimensional list to a CSV file.
When might you choose to use a CSV file instead of a standard text file? Maybe pause the video here whilst you have a think.
Izzy says, "If the data needed to be read in a spreadsheet software, I would use a CSV file." Lucas says, "I would use a CSV file if the data needed to be in tabular form." There's two really good reasons there.
Did you have any other reasons? Writing to a CSV file is very similar to writing to a standard text file.
The key thing to remember is how a CSV file is structured.
So here we have the example of a CSV file called players.
csv.
Each line in the CSV file represents a new row or record.
Each item in a row is separated by a comma.
The next thing to remember is that a CSV file is a text file.
You can only write data that is of the string data type.
Here is a programme that is attempting to write an integer to a CSV file.
So you can see on line two, we have a variable called numbers which holds the integer value 3.
On line three, we are attempting to write that value to the file, so file.
write(numbers).
When executed, this message will occur, TypeError: can only concatenate string, or str, not int, to str on line 3 of main.
py.
A list data structure is also not string data type.
You can't simply send a list data structure to a CSV file.
So here we have the list numbers, which holds the integer values 3, 4, and 5.
A similar message will occur if we try to run this programme.
So you can see the error message says that it can only concatenate string, not list data types.
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.
Each new row or record should be indicated by a line space, or the newline character.
Time to check your understanding.
What will be stored in the numbers.
csv file when this programme is executed? Pause the video whilst you have a think.
So, because the numbers are stored as a string data type, you can see from the view of the text editor, the values 3, 4, and 5 are added to the file with the commas in between.
If we have a look at a view from spreadsheet software, those values have been entered into the correct cells, so A1 contains 3, B1 contains 4, and C1 contains 5.
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.
What will you need to do to this list to make sure that it can be written to a CSV file? Maybe pause the video here whilst you have a think.
You'll need to change each item from an integer to a string.
You'll need to add each item to a single string, and then you'll need to add the comma in between each item.
To change the data type of each item, you can append to a new list whilst casting each item as a string.
So you can see here we have the original list, which is called numbers, which holds the integer values 3, 4, and 5.
On line 3, I'm initiating a new list, which is called str_numbers, and at the moment, that's an empty list.
I then use a for loop to loop through the original list and append each number to the new list, but this time casting it as a string value, so str_numbers.
append(str(number)).
So, we're creating a new list called str_numbers.
That's happening on line 3.
For every number in item in the numbers list, so that's our for loop, we're going to append the new list str_numbers with the return string number.
That's happening on line 5.
This will give you a new list based on the original list where each item is held with the string data type.
So now we can see what's contained in the list str_numbers.
Once you have a list containing items of the string data type, you need to turn the whole list into a single string.
Time to check your understanding.
Can you remember what the split method does? Pause the video whilst you have a think.
The split method converts a string to a list.
The string is split where a specified character, in this case a space, is found.
This is the join method.
It is a list method.
It does the exact opposite of the split method.
The join method takes a list and returns a string.
Items are joined together with a specified character.
In this case, we've used a hyphen as a specified character.
So you can see on the sample text output, we have the values, or the words, One, Two, Three, which are joined together now with the hyphen character.
You can leave the character empty to join the items directly next to each other.
So you can see now, on line 2 I have joined_words is equal to, I have open and close speech marks with nothing in between,.
join, and then words in brackets.
The sample output of this programme will be OneTwoThree with no spaces or special characters in between.
How can the join method help with this programme? Remember that you want the contents of the list to be written to the CSV file.
Pause the video maybe, whilst you have a think You can use the join method to create a string of items that are connected with a comma.
So you can see now on line 7, we have the line data = ",".
join(str_numbers).
Remember, str_numbers is our list of string values.
By joining the list into one string, you have a string that is ready to be written to the CSV file.
So now our data looks like this, 3, 4, and 5, all as a single string.
This creates a new CSV file with the data stored inside.
So you can see we've added lines 9 to 11, which are opening the file in write mode, writing the data, and then closing the file.
If we look at this view from a text editor, we have the values 3, 4, and 5 separated by commas, and if we view it from a spreadsheet, the values have been entered into the correct cells.
Okay, we're moving on to our first task of today's lesson, and you've done a fantastic job so far.
So well done.
The Fibonacci sequence is a well-known sequence of numbers where the previous two numbers are added together to make the next number in the sequence.
So if the sequence starts with zero, one, the next value will be one, because zero plus one is one, and so on.
So the sequence starts, zero, one, one, two, three, five, eight, and so on.
Create a programme that performs the following.
A, automatically populates the list with the Fibonacci number sequence up to 20 numbers.
B, writes the sequence to a CSV file so that the numbers appear on the top row of the file.
Hint, you will need to give the first two numbers of the Fibonacci sequence.
So fibonacci is equal to, open our list, 0, 1.
Close the list with the square brackets so that the programme can work out what will come next in the sequence.
Pause the video whilst you complete the activity.
How did you get on? Did you manage to create your Fibonacci sequence? Great work.
Let's have a look at some sample code together.
So on line 1, we have the hint code from the activity, so we have fibonacci = [0, 1] in that list.
On line 3, we have our for loop, which is going to go through and add up to 20 numbers in the Fibonacci sequence.
So for x in range(18) number = fibonacci[X] + fibonacci[X+1], so the first value plus the second value, and then we are appending that item to the end of the list, so fibonacci.
append(number).
Once we've finished with that for loop, we are then setting an empty list, which is going to be called str_fibonacci, 'cause we need to convert these values, these integer values, to string values in order to write them to our file.
So we have a for loop, similar to what we've used previous in the lesson for number in fibonacci: str_fibonacci.
append, and we're casting it as the string data type, so (str(number)).
On line 11, we're using our join method to put in our commas.
So data = ",".
join(str_fibonacci).
And then lastly, on lines 13 to line 15, we're opening the file and writing the data to the file.
So there we can see at the bottom of the screen a view from the text editor with the data added to the text file, fibonacci.
csv.
Did you manage to get your programme to write the correct numbers to the file? Remember, if you need to make any corrections, you can pause the video and do those now.
Okay, we're moving on to the second part of today's lesson and you're doing a fantastic job so far.
So well done.
We've already written data from a 1D list.
We're now going to write data from a 2D list to a CSV file.
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 2D list? Maybe pause the video whilst you have a think.
That's right.
A new line space or newline character is needed at the end of each row or list.
Using the same code from activity one on a 2D list will produce this CSV file.
So you can see that rather than having each list on a new row in the CSV file, it's added it all to the same row, which is not what we want.
A 2D list is a list of lists.
This programme is converting each list within the main list 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 the list.
So you can see the highlighting has now gone over each individual item rather than each individual 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, but you now have the added task of keeping the 2D list structure.
You still need to populate a new list, so in this example we've called it str_number_grid, with the string values.
This list must now be a 2D list instead of a 1D list.
This requires some extra steps.
For each item or row in the number grid, you need to create a new row, access the items in the original row, and return them as a string value to the new row, and then append that row, or list, to the new list, str_number_grid.
Izzy says, "Let's walk through the code." I think that's a great idea, Izzy.
So, on line 1, we're initiating the list number_grid, which contains a 2D list of lists.
So the first list contains the values 1, 2, and 3, the second list contains the values 4, 5, and 6, and the third list contains the values 7, 8, and 9.
On line 5, we're initiating the empty list str_number_grid.
On line 6, we start our first for loop.
So for item in number_grid.
So this is picking up the first list from number_grid.
We're then initiating an empty list called row, and then we go into our second for loop.
So for number in item, so this is going to look at the first number in item, which is number 1.
It's then going to append that number 1 as a string to our row.
So you can see that number 1 has now been added to row, but as a string with the speech marks around it.
We then go back through our for loop, this time looking at number 2, we append that as a string, back to our for loop, we look at number 3, we append that as a string type, we then move on to line 10, which is going to append row to our new list, which is str_number_grid.
We then go back through our for loop on line 6 to go into the second item in number grid, which is our second list.
We clear down row and initiate that as an empty list.
And we go to the first number in item, which is 4.
We append that as a string.
We go to the next item, which is 5.
We append that as a string.
We go to the next item and we append that as a string.
And then we go to line 10 to append row two, our list, str_number_grid.
And you can see it's now added that as a second list, so we're starting to have our 2D list of lists there.
Then we go back through our for loop for a final time.
So we're now looking at the last list in number_grid.
We're clearing down row.
We're looking at the first number in item, which is 7.
We're appending that as a string.
We're doing the next item, appending that as a string, and then the last item, which is 9, and we're appending that as a string.
And then finally, we run line 10 again to append that last list to str_number_grid.
You now have a 2D list containing string values.
This is not ready to be written to a CSV file, though.
What step do you still need to do to prepare it for the CSV file? Maybe pause the video here whilst you have a think.
That's right.
You need to join the 2D list into a single string.
The join method will not work on a 2D list.
This programme will cause an error message.
So line 12, the join method looks for a single item in a list to join as a string, which we don't have in this example.
Instead of a single item, it finds a list and it displays the error.
You can use the join method for each item or list in the 2D list.
So instead, we need to use a for loop to iterate through each item in the 2D list and join the strings together.
So on line 14, we have for x in range(3), because each list contains three values, data = data +",".
join(str_number_grid[X]), +, and then our new line character at the end.
Why is the newline character needed here? Maybe pause the video whilst you have a think.
That's right.
You need the newline character to add a new row after each record or list item.
Well done.
Okay, we then have the final step, and that's to write the string data to the CSV file like you have done before.
So line 17 to 19 have been added to write the contents to the CSV file.
Okay, we're moving on to our final task of today's lesson, and you've done a fantastic job, so well done.
I'd like you to create a programme that A, automatically populates a list that holds an 100 number square, B, writes the data from a 2D list to the CSV file.
The finished CSV file should look like the one on the screen.
Pause the video whilst you complete the activity.
How did you get on? Did you manage to create your number square? Great work.
Let's have a look at the sample solution together.
So on line 1, we're initiating an empty list, which is called number_square.
On line 3, we're setting the number variable to the integer value of 1.
On line 5, we have a nested for loop, which goes through and populates the number_square.
So we have for x in range(10), line = [], for y in range(10), so remember it's a 10 by 10 grid, line.
append(number), number = number + 1, so increment it, and then number_square.
append(line).
We then, on line 12, set a new list, which is called str_number_square, an empty list at this point, but this is going to hold our string values of the number_square.
So similar to what we did in the worked example for the lesson, we have a for loop which is going to go through and append each item as an str or string value to the list using that empty row, because we're using a 2D list.
On line 22, we have our final for loop, which is going through and using the join method to make sure that the items are joined using the comma, and we have our newline character.
And then finally, on line 25 to line 27, we're writing the data to the file like we've done before.
Remember, if you need to make any corrections to your programme, you can pause your video and do that now.
Okay, we've come to the end of today's lesson, Writing to CSV files, and you've done a great job, so well done.
Let's summarise what we've learned together in this lesson.
Writing to a CSV file happens line by line in the same way as other text files.
When writing to a CSV file, you need to convert the data to a single string with commas used as separators.
The join method takes a list and returns a string.
I hope you've enjoyed today's lesson, and I hope you'll join me again soon.
Bye.