[no audio]
Friends here we are going to see how to create a CSV file using python,
right? So, let me write a simple Python script just to create
a CSV file.
So whenever if you want to create a CSV file, first of all,
you need to import 'csv' module into your Python script.
Let me write a script name as 'create_csv_file.py'. So
I want to create a CSV file.
So I need to 'import csv' first, right?
So let me create any CSV file in any location.
Let me create a file in this location.
[no audio]
Right. Yeah. See that. Directly
you can take your required, suppose 'req_file' or required
CSV file, so some 'xyz.csv', but where you want to store this?
[no audio]
That's why it's better to provide complete path for your file.
So suppose I want to create my CSV file somewhere in this path.
So this is the path where I want to store my CSV file, and
then what is the name
you want to create here,
I mean for your CSV file?
Let's say 'demo.csv'.
That's it.
Right. See guys, if you have a CSV file, if you want to read the
content of your CSV file what you are doing? First, you are
opening your file
in a 'read' mode, but here we want to write the content into
your CSV file.
Then what we have to do is you need to open this in 'write'
mode. That's it.
Then finally we have to close it. Between this you need to
write the content into your CSV file.
[no audio]
Right. Fine.
See. Yeah, I will do one thing.
First of all, I am going to read the content, then based on
that I will try to convert that script.
I think you have a file called 'new_info'.
[no audio]
Right. Let me read the file content.
So we are going to create a object called 'reader' object,
so your 'csv_reader', or simply 'content', whatever it maybe.
So simply 'csv.reader'. So you are passing your object. Then
'delimiter=","'. Then 'for each_row', right, in your
'csv_reader', then you are able to print 'each_row'.
[no audio]
Yes, I'm going to get something.
What is that? What happened?
[no audio]
'req_file', yeah.
Sorry, that is a variable, right, I mentioned as a string.
That's not correct. Inside a variable
you have a file name.
Yeah, now it's good.
Right. Yeah, 'delimiter' for this file is '|', right?
Let me take '|' as my 'delimiter'.
[no audio]
So guys if you observe 'each_row' is like one list.
Right. See now
what I am doing is based on these steps,
so reading your CSV file is very easy, the same way if we convert
this into, to write the content to your CSV file then it's very
simple. Let me take now create, even I'm going to comment
this file as well.
[no audio]
Now just compare those steps and tell me, so I want to
create a file called 'demo.csv'.
So previously you opened your file for read, now
I want to open this file to write the content,
so 'req_file', 'w' mode. That's it.
So previously you created object called 'csv_reader' to read
the content from your file object.
But now I want to create a writer. Nothing
is there. Instead of 'reader', right?
See that. I want to create a 'csv_writer'.
You don't need to take this variable exactly. You can take any variable.
Right. So I am taking 'csv.writer'
for your file object, then let me take 'delimiter', as of now
I am going to take it as a ','; ',' or '|',
we will see for different symbols,
I mean different delimiters, right. Fine.
Now finally, you need to close your file.
Now you need to write some content into your CSV file, right?
See, whenever if you're reading the content from your CSV
file what you are getting? You're getting a list. The same way
whenever if you want to store the content into your CSV file,
each line must be like a row, that row must be like a list,
that list consists of your columns.
Nothing is there. Let's say I want to store my header, right?
Let me create a header, suppose,
so 'csv_writer.writerow'.
You have to write your row.
So what is our row? Our row must be a list as of now.
And that list consists of different values.
Each value is like one column in your CSV.
That's it. 'serial_number',
let me take 'name', and let me take 'age'.
[no audio]
That's it.
So as of now what I did? I created only header part.
I created only header part.
Let me save it and run it. It's done.
Let me go to my directory where I'm creating and there is
a file called 'demo',
let me open it. Instead of text file directly
I'm going to open it using your Excel.
Let me open it and see the data.
Yes, you're able to get your data. In your CSV file whatever
you have that you are able to open with your Excel sheet,
serial_number, name, and age. Not only with that
you can also open this file using some Notepad.
[no audio]
Yes, you are getting. By default
what is the separator you are getting? comma, you are not mentioning
comma to create your CSV file. By default if you don't mention
any delimiter,
then by default your 'csv' module will take delimiter as
comma. Guys, is this is not your delimiter.
This is the separator in your list. This comma and this comma, they
are not equal.
Right. See that.
What I am doing is, now instead of this one,
let me close this file, now
I am going to take delimiter as '|'.
[no audio]
Now save it, and run it, and see the result. Open your file.
I am going to open it using first of all with your 'EditPlus'.
[no audio]
See, now your separator you're getting automatically. Your delimiter
or separator between columns you are getting as '|'
symbol. That's it.
Now, we know that in case you have a '|' symbol to separate
your columns
then your Excel sheet can't open directly, can't separate your
columns. You know, you have to separate it by selecting your
column, then 'Data->Text to Columns', right, then 'Delimited', then
instead of tab or colon or semicolon or comma you have
to provide tab,
sorry '|' symbol,
I mean whatever the delimiter you are using to create, that's
it. So that it will separate.
That's fine.
Now as of now how many rows you are writing? Only header part
you're, you have written here, right? Now
let me take ',' as the separator, you can take anything.
Now I want to write some first row as well.
So just take this object '.writerow', then again provide
a list which consists of suppose, serial_number-1, name-
let's say some 'John', age-let's say 23. Save it and run it.
Go and open your file.
[no audio]
See the output. You have a small confusion here.
Yes, your data is there here but the thing is you are also
getting an empty line here.
Right. So guys if you are working with Python 3 then
let me show you that. Let me close this file. In your script while
opening your file
you have to write 'new_line=""', whereas in Python 2
just write, instead of this no
just write 'wb' mode.
That's it. And this is for Python 2. In Python 3
[no audio]
you have to write in this way.
Now, let me save it and rerun.
What happened?
[no audio]
Sorry.
[no audio]
Yeah, 'newLine'. That's a single word.
Not underscore, right. Now open your file here.
Now see the result.
Now, you don't have any empty lines in your CSV file.
It's working perfectly.
So there is a small difference between Python 3 and 2.
In Python 3
you have to write 'newLine=""',
whereas in Python 2 you have to write simply 'wb', 'write in
binary'. Right. That's fine.
In case if you want to write one more line, I mean one more
row again what you have to take? You have to take 'writer' then 'writerow',
[no audio]
then provide a list, that list consists of your number of
columns, right? Let's say, so this is the data
I want to store. Save it and run it.
Open your file and see the result.
[no audio]
Now, in case, so actually this operation, I mean 'writerow' is
helpful while writing your real-time scripts.
Suppose assume that you are going to work with some multiple servers.
I need to get the content of each and every server like, let's
say my CSV file should be in this way, serial_number, server_name,
and what is the os_name for that,
and what is the up_time for that, right, suppose what is the
version of that.
So 1, serial_number-2, 3, 4; server_name, os_name, what is the
version, what is the up_time,
what are the applications running,
what is the Java version.
Right. I need to write one by one server information into your CSV
file. We will see this type of script while going forward.
[no audio]
Right. So as of now in case if you have a data of each row, if you
want to write that row, you can write by using 'writerow',
'writerow' operation. Instead of that suppose
I want to write at a time all rows,
I want to write at a time all rows into my CSV in a single shot.
[no audio]
Then yeah, you have one more operation called
'writerows', then provide your rows as a list of lists values.
[no audio]
If you remember while printing list of your 'reader' object
what you are getting? You are getting a list which consists
of some number of values and each value is again a list of
values. Right. Now the same way in a reverse way
we are going to write.
See nothing is there. Very simple. Let me take first 'my_data', as I am taking
you can also take in a variable also. 'my_data' a list which
consists of - first value has a list of your header.
Your header has a list as a first value. Second value,
this information again as a list. Second value is also one
row. Row is nothing
but the list. You need to remember that. Then,
That's it.
Now, let me save it, and before that
let me delete our file. Even though if it is there no problem. You
know, if it is in 'write' mode your previous content will be
deleted and new data will be written.
[no audio]
Yeah, now I am going to save it and run it.
What happened?
[no audio]
Oh, sorry 'csv_writer.writerows()'.
So what is your data?
So I have to provide that. I missed that. 'my_data', a list which
consists of values. Each value is like one row. Row is nothing
but a list in your CSV file. That's it. Save it and run it.
Open your location and see the data. Yes, 'demo'
is there. Open it and see the content.
Right. See automatically you are able to get your data here. That's it.
So if you want to store at a time, your all rows,
yes, you can do it using in this way also.
[no audio]
Right. So instead of this if you want to write one by one row, whenever
if you're getting information about suppose particular server,
then you can also use this operation.
Right. So most of the times I will use this operation,
or you can also use this one also. Just to write your data
at at time, all your rows, right?
So based on situation we will use any one of that.
Okay, we will see that one real-time scenario to use effectively
this CSV file while going forward, right.
Okay guys, this is the way how to write a CSV file using
your Python, or how to create a CSV file using your Python. Okay.
Okay, thank you for watching this video.
[no audio]