Hello folks....!!!
Today let us look at how to write to CSV files.
To write to csv file, we have to open the file in 'w' mode using with open( ). Once opened, the function that is responsible for writing into the file expects a 1-D array or multi dimensional array of items depending upon the function used. Like reader() we have a writer() in csv. Let us look with examples.
writerow( ) and writerows( )
Let us look at an example first and then move on to explanation.
- csv.writer(file) creates a writer object.
- Using that object one can access the writerow() function.
- This function is used to write a single row to the csv file and thus expects a single dimensional array as parameter.
- For the example given above, I am storing some student record into a csv file. The details are roll number, name and marks scored. So I am writing the column names as the first row in the csv though it is not mandatory.
- For writing each student's record, the writerow() function has to be called.
- Upon successful completion of writing, the message is printed and the csv file looks like given below in the specified location.
Instead of calling the writerow() every time to write a single row, the csv module has another
function writerows() which allows multiple rows to be written at the same time. This
function expects a multi dimensional list (lists of lists) as the parameter.
Look at the code snippet below.
Now, the student records to be written are stored in multi dimensional list
first. This list is passed as parameter for the
writerows() function.
The parameter like delimiter,quoting,dialect which we applied for the
reader() applies to the
writerow() and
writerows() functions as well. To recall what these parameters do, visit the
previous page.
Customizing the writing
Below is a simple example to show how to format while writing to csv file
using the above said parameters.
The csv file looks like the one given below:
In the example above, in the dialect registration, I have passed a value
QUOTE_NONNUMERIC for the quoting
parameter. For reader() we used QUOTE_NONE. Let us look what it means in
detail. This quoting parameter can accept 4 values among which QUOTE_NONE is
the one suits the most for reader(). It can be used with writer() also.
-
QUOTE_ALL
:
Irrespective of the data type all values will be present within
quotes in the csv file.
-
QUOTE_MINIMAL :
The fields that contain special characters like quotes or any
delimiter will appear within quotes in the csv file.
-
QUOTE_NONNUMERIC :
Only the fields that are not numerical values will be inside
quotes.
-
QUOTE_NONE :
Nothing will be quoted.
DictWriter( )
The DictWriter() is used to write a Python dictionary object to the csv
file. The syntax is:
csv.DictWriter( d, field_names, restval, extrasaction, dialect)
Parameters and explanation:
- d : The dictionary object to be written to the file.
- field_names : The column names stored in a list.
- restval : The value to be written in case of missing key.
- extrasaction : The action to be done when an extra key-value pair is found that is not mentioned in the field_names. Mostly 'raise' is given as value which means raise exception message. If 'ignore' is given that value is ignored.
- dialect : For specifying the formatting constraints.
Let us look at an example:
example 1 : A simple students record.
The output will be a file as given below:
Instead of a writer() we use DictWriter() here. Now let us look at the other
parameters and how they work.
example 2- restval parameter
Note that, the mark field is not given for roll number 2. So The default
value for left out values can be set using restval parameter. Here I have
set it to empty quotes (' '), Which means no value should be filled. The
resultant csv file will look like the one given below.
You can pass any value, a '0' or 'NA' or 'NIL' - anything that suits the
further process you are going to do.
example 3- extrasaction parameter
Here I have added a key-value pair 'Grade' : 'B' in roll number 2.
This 'Grade' is not given in the field names list. Thus it is an extra
value. Now setting extrasaction to raise, will throw you an exception
as given below:
If you just want to ignore all the extra values, then you can set it
'ignore' just as given below:
Now the extra value is skipped and you will get a csv file as given below:
These are the essential things and the frequently used concepts in csv
package. For more deeper knowledge visit
this link
or download the material given in
Downloadable docs .
Next Topic👉
Comments
Post a Comment