Writing and Reading in CSV Files
Writing and Reading in CSV Files
Writing and Reading in CSV Files
• To write to a CSV file in Python, we can use the csv.writer() function. The
csv.writer() function returns a writer object that converts the user's data into a
delimited string.
• This string can later be used to write into CSV files using the writerow() function.
• In order to write to a CSV file, we create a special type of object to write to the
CSV file "writer object", which is defined in the CSV module, and which we
create using the writer() function.
• The writerow() method allows us to write a list of fields to the file.
• The fields can be strings or numbers or both.
• While using writerow(), you do not need to add a new line character (or other EOL
indicator) to indicate the end of the line; writerow() does it for you as necessary.
Let us write data onto a CSV file using writerow() method.
In the above program, the very first line is for importing csv file into your program.
Next, whatever are the column headings for our data are mentioned as a list in the variable called fields.
All the data stored inside these fields is placed inside the variable called rows.
Now give the name of your file, let us say, marks.csv. This will be created and stored inside your current working
directory
‘w' stands for write mode and we are using the file by opening it using open()
The next statement comprises the most important function used for writing onto csv file, viz. csv.writer(), to obtain
a writer object and store it in the variable csv_w as the name of the variable, and this is the CSV object
writer() takes the name of file object ‘fileobj' as the argument. By default, the delimiter is comma (,).
Using the newline parameter allows the csv module to handle the line endings itself
• writerow(fields) is going to write the fields which are the column headings into the file and have to be written only
once.
• Using for loop, rows are traversed from the list of rows from the file.
• writerow(i) is writing the data row-wise in the for loop and in the end the file is automatically closed.
giving csv.writer(), the delimiter taken is comma.
• We can change the delimiter whenever and wherever required by changing the argument passed to delimiter attribute.
For example
delimiter = "|" (pipe symbol). You can put any character as delimiter and if nothing is given, comma is placed by default.
writerow() method is used to write each row.
We can avoid using for loop and can write all
the rows/records in one go.
This can be done by using writerows()
method. writerows writes all the rows in one
go, so you need not use for loop and
iterations.
Reading records from csv file
As seen from the above code , every record is stored in reader object in
the form of a List.
In the above code, we first open the CSV file in read mode.
The file object is named fileobj.
The file object is converted to csv.reader object.
We save the object as csv_r.
The reader object is used to read records as lists from a csv file.
'''reader() The reader object is used to read from a CSV 0
file.
The CSV module includes a reader() method that can be
used to read a
CSV file into our program. The
reader function converts each line of a specified file into
a list of columns.'''
QUESTION
Vedansh is a Python programmer working in a school. For the Annual Sports Event, he has
created a csv file named Result.csv, to store the results of students in different sports events.
The structure of Result.csv is : [St_Id, St_Name, Game_Name, Result]
Where St_Id is Student ID (integer)
ST_name is Student Name (string)
Game_Name is name of game in which student is participating(string)
Result is result of the game whose value can be either 'Won', 'Lost' or 'Tie’
For efficiently maintaining data of the event, Vedansh wants to write the following user
defined functions:
Accept() – to accept a record from the user and add it to the file Result.csv. The column
headings should also be added on top of the csv file.
wonCount() – to count the number of students who have won any event. As a Python
expert, help him complete the task.