[no audio]
Friends, here we are going to work with CSV files using Python.
So first of all, what is CSV?
See guys simply, CSV is a Comma Separated Values.
That means whatever the values you have in a CSV file
they all are separated by a comma, or separator is comma,
or delimiter is comma. Separator or delimiter is same, right.
Fine. Now why we are using this? So you are going to use this
just to store tabular type of data,
such as your Excel or database data. Of course, we can store
our data as a tabular form in your Excel sheets as well.
But compared to excel sheets, CSV is the simple way to store
your tabular type of data.
Let me do one thing.
First of all, I am going to open a simple file.
Let me open a file called, simply any text file
I am going to open it.
Right. Fine. See that.
Let me open it first.
See by default your CSV data is separated with comma. So
basically CSV, you can assume that CSV is like your Excel sheet,
or tabular data. So the tabular data is nothing but you have
rows and columns. Your data,
your data is like a rows and column.
Let's say I have suppose, column as serial number,
name, suppose salary, then skill, so I am going to store in
this way. Suppose
let's say 'John', 'Python',
sorry. 'salary', right. Some $2000.
Let's say 'Python'. Then I have 'Jason',
then $1000,
then I have suppose, Bash.
Let me save it as, as a CSV file.
Some 'info.csv', that's it.
So I saved your file as '.csv' extension, as a CSV file.
See now this is your CSV file.
I can open this CSV file with any 'Notepad', right?
See that. Your data
is there as it is, or by default I can able to open that using
your Excel sheets as well.
See that, automatically your data is arranged in terms of
rows and columns.
In terms of rows and columns.
So guys if you observe while storing your data, right, we
stored our data by taking comma as the separator between your columns.
Right. Not only that, you can also store your data by taking
any other delimiter as well.
Let's say, I am going to take again same data with a delimiter as
something like '|' 'serial number', name', suppose 'salary',
then I am going to take 'skill'.
Let's say serial number - 1,
'John', I'm taking salary suppose $2000, 'skill' -
let's say Python. Same way
I am going to store one more data.
You can take anything.
So here my intention is, in case if we take separator as other
than comma how you can work with your CSV files, that's important.
Let me now save this file as some 'new_info.csv'.
Let me save it as '.csv' file.
Let's say 'name' as 'new_info.csv'.
That's it.
Now previously you stored your data by taking separator as comma,
but now I have taken separator as pipeline. By default
if I open that, see that, now you are not getting your data
in terms of columns.
See that your Excel sheet is not able to identify your data
as in terms of columns.
It has given only one column. But you know the delimiter, right?
What is the delimiter you used to create your CSV file?
Just select your column, go to 'Data-> Text to Columns'.
[no audio]
delimiter, 'Delimited', right. Just select other, and provide
your '|' as. See that. Now you're able to fetch your
data, I mean you are able to convert your data into number
of columns based on the separator
you used for your data.
[no audio]
So this is also one type of encoding.
See, if you know pipeline then only you can able to retrieve
the data from your file.
So if third person don't know what is the delimiter you
used for your data, then they can't separate your data.
Right. So it's like a one type of encoding. Of course, not that much
good but up to certain limits
it is good practice.
That's fine. And guys if you store your data in CSV file, it is very easy
to transfer your data to your database, or from database to
if you want to download it is very easy to download into CSV files.
That's why in real time
most of the cases you will work with CSV files instead of
excel sheets.
[no audio]
That's fine. Now guys assume that you have one CSV file.
Now you want to read the data using python. Then how you can read it?
Right. See let me save a script name called 'read_csv'.
Anything you can take it. Now,
I am going to take it,
I'm going to store somewhere.
You can store anywhere.
Let's say I want to store here.
'read_csv_file.py'. So guys if you want to read a CSV
file, or if you want to write a data into your CSV file, in
your python you have a default module called CSV'. First you need to
import that. This is the default module. You can directly use
that. Then let me take 'req_file' to read. So suppose that
file is, suppose
let's say 'info.csv', right. But guys whenever if you are
working with files, it is a good practice to provide complete path.
[no audio]
Right. See, let me take the complete path for my file.
So actually it is there in somewhere, right?
So I'm going to provide complete path for my file.
And guys you are working with Windows,
that's why please, please provide '\\' in your path.
[no audio]
Yeah, fine. Now you have in your hand your CSV file.
Next you need to read it.
So guys forget about the CSV,
if you want to read a text file, how we can read? Already we
have seen our previous videos, right?
Let's say I'm going to open your file in 'read' mode. Your file
is there in the variable called 'req_file'
I want to read the content, then open your file in 'read' mode.
Finally just to close your file.
Now I want to get the data
what is there with your CSV file,
I mean with your file, so 'content' or 'data' equals to simply
'fo.', I am taking 'readlines'.
[no audio]
Then let me print your data.
So I am going to take one by one line from your 'content'.
Then, I want to print that. See the result.
[no audio]
Yes, you are getting your data line by line.
Right. So, you know, if you want to remove that space, empty
line, you can strip it.
Yes, you are getting. But the thing is this is like a string.
String means this entire data is like a one column but we
know CSV is nothing but tabular data.
So now you are not getting your data as a separate columns.
Your entire line you're
getting as a one line only. I should not get like that.
Because it is a CSV,
I need to get in terms of columns.
[no audio]
Right. Now what you have to do is first you have to read your, you
have to open your file in 'read' mode, then whatever the data
you have inside of your file that data
you need to convert it into
[no audio]
rows and columns.
Anyway you're getting rows, but you need to convert that
data into columns.
That's why what you have to do is, you need to parse, you need
to send your data into CSV,
so that CSV will convert your data in terms of columns.
Right. So now see that what I'm doing is 'csv_reader', no need to take
this much lengthy variable,
simply you can take, 'csv', 'csv_reader', 'cr', or simply
'csv', or, 'data', equals to, what you have to do is from CSV module,
right, use a function called 'reader'.
So from your file try to convert your rows,
anyway file is nothing but, normal file is also you're going
to get line by line. Line is nothing but a row in your CSV.
But that line consists of, that line consists of columns.
So CSV will automatically convert your data into columns,
right. Now see the result.
[no audio]
So guys in your data you have all your rows of your CSV in
terms of columns.
Let me take, suppose if I directly print your 'data'.
So that is the object, but inside of that object you have
number of rows and each row consists of some columns.
So if you want to see that, 'for each in data', let's say,
I want to print that. Now see the result.
What happened?
'for each in data'.
[no audio]
oh, we close that, right? Yeah.
[no audio]
Let me close after this.
[no audio]
Now see that.
You are able to get your data.
See, each line has a list. List means number of values, separate
values. That means this is one column,
this is one column, this is
one column, separate columns you are getting.
[no audio]
Right. Separate columns you are getting,
or in case, in case if you
don't want to take this, if you don't want to use your CSV,
right, see what I am doing is let's say I want the content
of my file, you know 'fo.readlines'. Without CSV module
also you can convert your data,
[no audio]
into number of columns. See that. I am going to print,
first of all your data line by line. Yes, you are getting.
But you know, this is the a single row. In this do you have
four columns. Now,
if you observe here, this string somewhere you have comma.
I can split it no,
simply. Actually this operation is doing by your CSV module.
That's it. Now see the result. The same data what you are getting from
your CSV module same thing you can able to get it but no
need to do this split operation by yourself, that will taken
care by your CSV module.
That's why what we are doing is,
[no audio]
right, see that.
[no audio]
Now 'content' is nothing but from your 'csv' module create a
'reader' object, or convert your string, string of rows into
some columns.
So for that you can do in this way. That's it.
Now 'for each in content'. Right.
[no audio]
Let me 'print(each)'.
[no audio]
Now see the result. Yeah, finally
you need to close your file.
Don't forget about that.
'fo.close'.
[no audio]
That's it.
Right. So guys actually CSV is splitting your data based
on delimiter, but we are not providing anywhere delimiter,
right. Then how your CSV module is splitting your line into
a list which consists of your values as columns?
[no audio]
We know by default CSV will consider separator as comma.
CSV is nothing but Comma Separated Values. But in case if
you want to mention to your CSV, if you want to tell to your
CSV, "Use this as a separator, delimiter". You can mention, 'delimiter'
is nothing but ",". That's it. Now see the result.
[no audio]
Right. Suppose if I don't mention this, and anyway by default
your delimiter is comma, by default with your CSV
that's why CSV will assume that comma is the separator.
Now we have one more file, right?
If you observe this file, 'new_info', in that delimiter is not a comma.
Let me try with that 'new_info'.
[no audio]
You're getting your data as one value on your list.
That means again, you have one column.
Actually these are four columns in each line, right?
But you're getting as one column. Then in this case, you
need a delimiter.
So for better practice always try to write 'delimiter' as your required thing.
I mean you just mention your 'delimiter' option as well while
converting your data into rows and columns.
That's it.
Now it is not separated, comma as the separator,
it has a pipeline as a separator. Now, let me run it.
What happened? Sorry
[no audio]
That's it.
'delimiter'. So likewise, whatever the delimiter you have in your CSV
file, if you know that you can able to read your data using
your Python very easily in this way.
[no audio]
Right. Fine.
So this is the way how we can read your content from your
CSV file.
So guys nothing is there.
Just open your CSV file in 'read' mode like a text file, after
that you are going to convert your data into tabular data
by using this operation from CSV '.reader', then provide
your file object information,
and what is the delimiter in your CSV file.
Then you are going to get content. Then in that content
you have all your rows and columns. Then you need to take
one by one row.
See, if it is a CSV file, right, it is having rows. Each row is having
some columns.
So all rows if I, I can take by using 'for' loop from your 'content',
not only 'content', you can take any variable here.
Right.
So after reading that then you can close your file.
[no audio]
Okay. So guys this is a simple way to read your CSV files.
While going forward we will
see different ways to work with your CSV files.
Okay. In next video
we'll see how to write a content to your CSV file.
Okay.
Okay guys. Thank you for watching this video.
[no audio]