0% found this document useful (0 votes)
4 views

CSV File Handling Doc

A CSV (Comma Separated Values) file is a plain text document used to organize tabular data, with each row representing a data log and fields separated by commas. The document explains how to read from and write to CSV files using Python's csv module, detailing methods like reader() for reading and writerow()/writerows() for writing data. It emphasizes the importance of the .csv file extension and the use of the 'with' keyword for better file handling.

Uploaded by

namancj04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

CSV File Handling Doc

A CSV (Comma Separated Values) file is a plain text document used to organize tabular data, with each row representing a data log and fields separated by commas. The document explains how to read from and write to CSV files using Python's csv module, detailing methods like reader() for reading and writerow()/writerows() for writing data. It emphasizes the importance of the .csv file extension and the use of the 'with' keyword for better file handling.

Uploaded by

namancj04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

A CSV (Comma Separated Values) file is a form of plain text document that uses a

particular format to organize tabular information. CSV file format is a bounded


text document that uses a comma to distinguish the values. Every row in the
document is a data log. Each log is composed of one or more fields, divided by
commas. It is the most popular file format for importing and exporting spreadsheets
and databases.
CSV format is the most common import and export format for spreadsheets and
databases. It is one of the most common methods for exchanging data between
applications and popular data format used in Data Science. It is supported by a wide
range of applications. A CSV file stores tabular data in which each data field is
separated by a delimiter(comma in most cases). To represent a CSV file, it must be
saved with the .csv file extension.

Reading from CSV file


Python contains a module called csv for the handling of CSV files. The reader class
from the module is used for reading data from a CSV file. At first, the CSV file is
opened using the open() method in ‘r’ mode(specifies read mode while opening a
file) which returns the file object then it is read by using the reader() method of CSV
module that returns the reader object that iterates throughout the lines in the
specified CSV document.

csv Module: The CSV module is one of the modules in Python that provides classes
for reading and writing tabular information in CSV file format.

Reading a CSV File Format in Python:


Consider the below CSV file named ‘Giants.CSV’:

Note: The ‘with’ keyword is used along with the open() method as it simplifies
exception handling and automatically closes the CSV file.
Example: This code reads and prints the contents of a CSV file
named ‘Giants.csv’ using the csv module in Python. It opens the file in read mode,
reads the lines, and prints them one by one using a for loop. The csv.reader() function
is used to read the CSV file, and the data from each row is printed to the console.
import csv
# opening the CSV file
File= open('Giants.csv', mode ='r')

# reading the CSV file


csvFile = csv.reader(file)

# displaying the contents of the CSV file


for lines in csvFile:
print(lines)

Writing to CSV file


Write into CSV Files Using csv.writer()
csv.writer class is used to insert data to the CSV file. This class returns a writer object
which is responsible for converting the user’s data into a delimited string. A csvfile
object should be opened with newline='' otherwise, newline characters inside the
quoted fields will not be interpreted correctly.

Syntax:
csv.writer(csvfile)

csv.writer class provides two methods for writing to CSV. They


are writerow() and writerows().

writerow(): This method writes a single row at a time. Field row can be written using
this method.
Syntax:
writerow()

writerows(): This method is used to write multiple rows at a time. This can be used
to write rows list.
Syntax:
writerows(rows)
# writing to CSV

import csv

# name of csv file

filename = "university_records.csv"

# writing to csv file

csvfile = open(filename, 'w')

# creating a csv writer object

csvwriter = csv.writer(csvfile)

# writing the fields

csvwriter.writerow(['Name', 'Branch', 'Year', 'CGPA'])

# writing the data rows

csvwriter.writerows([ ['Nikhil', 'COE', '2', '9.0'], ['Sanchit', 'COE', '2', '9.1'], ['Aditya', 'IT', '2',
'9.3'], ['Sagar', 'SE', '1', '9.5'], ['Prateek', 'MCE', '3', '7.8'], ['Sahil', 'EP', '2', '9.1']])

You might also like