Computer Science Project
Computer Science Project
Computer Science Project
Topic :-
Data File Handling In Python
CSV (Comma Separated Values)
Name :- Sarthak Gupta
Class :- XII-S
Roll No. :- 12442
COMMA SEPERATED VALUES (CSV)
INTRODUCTION TO
CSV
In today’s organizational working network, data sharing is one of the major tasks to be carried out, largely
through spreadsheets or databases.
A basic approach to share data is through the COMMA SEPERATED VALUES (CSV) file.
CSV is a simple flat file in a human readable format which is extensively used to store tabular data, in a
spreadsheet or database.
A CSV file stores tabular data (numbers and text) in plain text.
Files in the CSV format can be imported to and exported from programs that store data in tables, such as
Microsoft Excel or Open Office Calc.
CSV file is a delimited text file which uses a comma to separate values.
Each line in a file is known as data or record.
Each record consists of one or more fields , separated by commas (also known as delimiters), i.e. each of
the records is also a part of this file.
Tabular data is stored as text in a CSV file.
It stores our data into a spreadsheet or a database.
The use of comma as a field separator is the source of the name for this file format.
WHY USE
CSV?
The extensive use of social networking sites and their various associated applications requires the handling of huge
data , storing huge and exponentially growing data sets, processing data having complex structure (structured,
un - structured , semi - structured), bringing huge amount of data to computation until become a bottleneck.
The solution to the above problem is CSV. Thus, CSV organizes data into a structured form and, hence, the proper
and systematic organization of this large amount of data is done by CSV. Since CSV file formats are of plain text
format, it makes it very easy for website developers to create applications that implement CSV.
CSV files are commonly used because they are easy to read and manage, small in size, and fast to process/transfer.
Because of these salient features, they are frequently used in software applications, ranging anywhere from online
E- commerce stores to mobile apps to desktop tools. For example, Magento, an E-commerce platform, is known for
its support of CSV.
ADVANTAGES OF
CSV
For working with CSV files in python, there is an inbuilt module called csv . It is used to read and write
tabular data in CSV format. Therefore, to perform read and write operations with CSV file, we must import
csv module. Csv module can handle CSV files correctly regardless of the operating system on which the
files were created.
Along with this module, open() function is used to open a CSV file and return file object. We load the
module in the usual way using import
>>> import csv
OPENING AND CLOSING CSV FILE
SYNTAX:
<file variable/file object>=open(“file_name” , ”access_mode”)
Here, the first argument with open() is the name of the file to be opened and the second argument
describes the mode i.e. how the file will be used throughout the program. This is an optional parameter as
the default mode is read mode.
Open function takes the name of the file as the first argument. The second argument indicates the mode of
accessing the file.
SYNTAX:
fileobject.close()
The close method of file object flushes any unreturn statement and closes the file object, after which no more
writing can be done. Python automatically closes the file when the reference object of a file is reassigned to
another file but it is good practice to use the close() method to close the file.
FILE MODES IN CSV
1) ‘r’: Opens as file for reading only. The file pointer is placed at the beginning of the file. If the file
doesn’t exist it will generate FileNotFoundError.
2) ‘rb’: Opens a file for reading in binary format.
3) ‘r+’: Opens a file for both reading and writing. The file pointer will be at the beginning of the file.
4) ‘rb+’: Opens a file for both reading and writing in binary format.
5) ‘w’: Opens a file for writing only. Overrides the file if the file exist. If the file doesn’t exists, it
creates a new file for writing.
6) ‘wb’: Opens a file for writing in binary format.
7) ‘w+’: Opens a file for both reading and writing.
8) ‘wb+’: Opens a file for both reading and writing in binary format.
9) ‘a’: Opens a file for appending in binary format. The file pointer is at the end of the file if the file
exist. If the file doesn’t exist, it creates a new file.
10) ‘a+’: It opens the file for both reading and appending. The file pointer is at the end of the file if the
file exists. If doesn’t exist , it creates a new file for reading and writing.
11) ‘ab+’: It opens a file for both appending and reading in binary format.
OPERATIONS ON A CSV FILE
There are 2 basic operations that can be carried out on a CSV file:
1) Reading from a CSV file
2) Writing to a CSV file
READING FROM A CSV FILE
Reading from a file is done using a reader object. The CSV file is opened as a text file with
python inbuilt open() function, which returns a file object. This creates a special type of object
to access the CSV file (reader object) using the reader function.
Code:
import csv
f=open("student.csv","r")
rows=csv.reader(f)
for r in rows:
print(r)
f.close()
PRACTICAL IMPLEMENTATION-2
Code:
import csv
fields=['Name','Class','Year','Percent']
row=[['Student1','XII-S','2023','89.5'],['Student2','XII-S','2023','90']]
with open("marks.csv","w",newline='')as f:
csv_w=csv.writer(f,delimiter=';')
csv_w.writerow(fields)
csv_w.writerows(row)