- [Instructor] In this and the next several videos,
we'll be taking a look at our next
Intro to Data Science section
on working with CSV files or comma-separated value files.
Now, many of the most popular datasets
for learning data science
and artificial intelligence techniques
are provided in comma-separated value form,
so it is quite handy to understand
how to process comma-separated value files.
Now, it also turns out that a lot of the datasets
we will be working with in higher-end lessons
are bundled with the various libraries
that we'll be presenting,
and they're often bundled in such a way
that you can load up datasets
with a simple line of code that handles all the details
of processing the comma-separated value files for you.
So, although I am going to show you some basics here,
initially, of working with comma-separated value files,
a lot of times that will be hidden from you
by the libraries that you're using.
So, in this video in particular,
we're going to start by presenting
some basic capabilities of the Python
standard library module CSV,
which stands for comma-separated values.
So, I'm gonna go ahead and import that,
and to start out, what I want to demonstrate
is writing to a CSV file.
So, for that purpose, I've gone ahead
and pasted in now a with statement
that's very similar to the text file processing statements
that we demonstrated in earlier videos of this lesson.
The first thing we're going to do is open up a file,
and in this case, we've specified accounts.csv.
CSV is a common file name extension
for comma-separated value files.
Now, we're opening this with the w mode
so if the file does not exist, it will be created.
If it does exist, it will be wiped out,
and we will be writing a new file from scratch.
And if you look at the CSV module's
online documentation at python.org,
you'll see that they recommend
as a third argument to the open function
newline= empty string.
And what this is going to do is enable
the CSV module to properly handle
new line characters on its own.
That's actually built into the module.
If you do not include this,
then new lines will not be handled correctly
across all platforms, and that may result
in some problems in your CSV handling code.
So, we're going to open, in this case,
accounts.csv as accounts, so that will be
the variable name we use to refer to the file object,
and this is similar in concept
to what we did to write into a text file,
but the first thing we do in the context
of CSV files is obtain a writer.
So, there's a function in the CSV module called writer.
You give it a file object as an argument,
and it gives you back a new object
called the writer that is used to write
specifically comma-separated values into that text file.
Now, in this case, we've chosen to write
five separate writerow statements.
Every writerow statement is going to create
a line of text in the comma-separated value file,
and the argument to writerow is a sequence of values
that will be output as comma-separated values.
So, we're using the same records of information
that we demonstrated earlier
when we introduced text file processing.
So, let's go ahead and execute this with statement,
and at this point, we've now created
the comma-separated value file
and remember that you can use !cat,
or !more if you're on Windows,
to view the contents of the file that you just created
and make sure it actually got written correctly.
So, let's go ahead and take a look
at the accounts.csv file here, and you can see that, indeed,
we have comma-separated values.
Now, we happen to have written values,
in particular for these strings,
that were one string with no space characters
or other information in them.
If you had, for example,
Jones, up here, Jones comma Sue,
then the
CSV module would actually output that
with double quote characters around the entire field.
So, if there are commas in the strings
that you are writing out,
they will be enclosed in double quote characters
automatically by the CSV module,
and similarly, when you read in that CSV file,
it will see those double quote characters
and know to treat everything within them
as one field of information to read in.
Now, another thing I want to point out
about these five statements, here again,
we chose writerow, which writes one row at a time,
but there's also a method, or a function rather,
called writerows, which can receive
a nested list, for example, a list of lists
and write every row of that list
as a separate line of text in the file.
So, we could have combined all of this
into a single statement if we wanted.
So, now that we've written the data out to the CSV file,
let's also prove that we can read that data back in.
And, again, we'll use the CSV module for that.
Here, I've pasted in a with statement
that's going to open up the file, accounts.csv.
Remember, it was automatically closed
by the first with statement.
We're gonna open it for reading,
and again, the online docs for this module
say you should always include newline= empty string
as a third argument when you're opening
a comma-separated value file to ensure
that the new line characters get processed correctly.
And we will call this file accounts.
We're going to print out some column heads
so that we can produce nice looking output
for this part of our demonstration.
And similar to what we did up above here,
when we write to a comma-separated value file,
we use the file object to create a writer,
while when we're going to read from it,
we use the file object, accounts,
to create a reader that specifically knows
it's looking for comma-separated values
and will parse them automatically.
So, we create our reader, and just like with a file
where you can iterate through the file
one line at a time in a for-loop,
you can iterate through the CSV reader one line at a time.
And, in our case, we know that every one
of our comma-separated value rows has three values in it,
so we're taking the record that we're reading,
and we're splitting it into three pieces of information:
the account, the name, and the balance.
And we'll then display the account, the name,
and the balance individually in a formatted string.
So, let's go ahead and execute that,
and as you can see, we got the five rows of information.
We can confirm by looking back up above here
that the information is correct and in the right order,
and we also got the nice column-based formatting
because we were using the f-string notation
that you've seen previously.
So, as you can see, it's real easy in Python
using the CSV module to both create
comma-separated value files and read them in as well.