Welcome back.
It's time to learn about CSV or CSV files.
Those are Excel files that we're going to try and create our database in.
Now, luckily for us, Python has a CSV module and we're going to use this CSV module to do the things
that we're going to do, which is to write instead of our text file to a CSV file.
Now if we keep scrolling down here.
You can read all about the CSP module and how to use it, but instead of going through it line by line,
let's just code it and show you exactly how it works.
So I'm going to create a new file and this time it's going to be called database dot CSV.
And let's go back to our server pie.
And instead of having the right to file function.
Let's write another function which will be the right to CSV.
And this one will be a little bit different because we have to use the CSV module.
So I'm going to actually import it.
If we go to the documentation, you can see that we can import it just like this import CSV that comes
built in with Python.
Now in here.
We're going to do something similar, except we're going to open the file.
That is the CSV file.
We're going to append to this file.
And we're just going to call it database or in our case, database, too.
So let's just say database to here.
Now we have our data that we receive.
But instead of a file, now we have a CSV or a CSV.
Writer.
So we're going to do a few different things here.
First we're going to write in our CSV these columns now with a CSV file by using these commas.
So we have the email column, the subject column and the message column and the commas essentially say
this is the end of this column.
This is the end of this column, and this is the third column.
So we can keep it the way that we have in the database dot text file.
Now in here.
Let's remove what we have here.
And we're going to say CCV dot, right.
Or a writer to write to our CSV file.
And this writer takes different parameters that we can give it.
One is, where do we want to write this?
Well, we want to write to the database to that is the file that we just opened.
The second is a couple of options that we have.
Now, if we go to the documentation and we scroll all the way up.
You see that?
We have the writer, CSC, writer, and then a couple of options.
Now this delimiter.
Quote, character.
And quoting are just options that we can give it, that you can read on your own to tell it how we want
to write into this CSV file.
In our case, I want the delimiter.
To be the.
That is.
Each item in a row will be separated by a comma.
And whether we want any quotes around the characters.
Well, I don't want any.
So this is this could just be an empty string.
And then finally quoting.
Is going to be CSV.
DOT Well, we can just copy exactly what's over here.
Let's just copy quoting csv quote minimal.
And again, just to practice this.
Let's say you wanted to know what, quote, minimal means.
Again, we just look for that in the text and it looks like in structure.
Right.
Or object to only quote those fields which contain special characters.
So essentially it's saying don't wrap data in quotes unless we really, really need to.
Again, something that you don't have to worry about, this is more of a configuration that you can
just copy and paste and you can read up on your own what each of these mean.
The important part is the fact that we're writing to the database with some options.
So now we have this CSV writer object that we can use.
So this CSV writer can now be used like this.
We can write a row.
So right row and this right row will contain the data that we want.
And we have that right.
We want our right row exactly like we have above.
Like this.
Except the way we write, Rose, is that we give it.
Just each parameter.
So we want to give it the email variable which we have here, the subject variable and then the message
variable.
Just like that because we already have email, subject and message, and the CSB writer is smart enough
to say that these are going to be what we want and we just pass it in instead of parameters actually
as a list like so.
So that this row is going to contain email, subject and message that we extract from the data.
Now.
If we didn't make any mistakes, this should work.
But let's check this instead of write to file, let's do write to CSV.
I'll save.
Go open up the terminal.
Go back to our page.
And lets go to contact me again.
Let's do w at w dot com.
Some text, some text and hit send.
Oh.
We have a bit of an error here.
Quote, Character must be set if quoting and able.
So it looks like I have just a bit.
Of an error here.
And that is because we actually need a, quote, character to not be empty in here.
So if we're going to quote something, we just need to say, hey, what are we going to use?
And in our case, we can use the quote sign with the double apostrophe.
Again.
We can use whatever we want.
You might use this if you want, or this, whatever you prefer.
So let's just do a quote that we use in a string.
I'm going to save this.
Give it another go.
Let's refresh.
Actually, let's go back and submit again.
All right.
Thank you.
I'll get in touch with you shortly.
And if we go back to our database.
All right, we have our new data.
But you see over here that we don't have a new line.
Let's actually open this CSV file.
If I go to database CSV and I double click here.
All right.
It looks like we're getting our data, but our database, email and subject are a little bit off.
Ideally, these are the labels and headers and all the rows come after it.
Now let's go back and submit just a different user.
Let's go a w add a couple of more hits sent.
All right.
We're adding new rows, but the only issue is that the first time we add a new row, we should have
that extra spacing.
Now, this is an easy fix.
If we go to CSV and scroll a little bit down, we see that we have a right header method.
So what we'll do instead is actually just add a new line option and just give it an empty string.
So let's just clear everything.
Let's just clear everything in here.
We're going to save the database.
And just have server pie.
We're going to go back and submit the forms a couple of times.
Just to make sure that everything works.
If I go back and go to my database dot csv.
All right, we have our data.
If I open this now.
There we go.
We have our database right here with our headers and our entries so that now I can have as many entries
as possible and people can contact me.
And I'll always have this database for me to check to see if anybody wants to work with me.
Very, very cool.
But there's one other issue here.
That is right now we're just using an Excel file on the same machine as the server.
You see, there's a problem here because now my server and my database are in the same location.
So yeah, when the server stops, my file is still saved.
But what happens if maybe this computer breaks or something happens to it and I can't turn it on anymore?
I've lost my files.
Or maybe we have so much data that we want to store.
Well, this is where actual databases come in.
Databases specialize in storing information.
Large scale applications can use databases to store large amounts of data and then do really interesting
things with them.
Kind of like an Excel sheet, but even more powerful.
And on top of it, we can actually have a database on a completely different machine, maybe on multiple
machines, so that we're guaranteed not to lose that data.