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

Chapter 5.3 CSV File Handling

The document provides an overview of CSV (Comma Separated Values) files, detailing their structure, advantages, and manipulation in Python. It explains how to open, write, read, and close CSV files using Python's csv module, along with examples of code for each operation. Additionally, it outlines methods for updating, adding, and deleting records in a CSV file.

Uploaded by

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

Chapter 5.3 CSV File Handling

The document provides an overview of CSV (Comma Separated Values) files, detailing their structure, advantages, and manipulation in Python. It explains how to open, write, read, and close CSV files using Python's csv module, along with examples of code for each operation. Additionally, it outlines methods for updating, adding, and deleting records in a CSV file.

Uploaded by

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

1. What is CSV File................................................................................................................................

2
2. Structure of a CSV File....................................................................................................................2
3. Advantages of CSV File...................................................................................................................3
4. CSV File manipulation in Python.................................................................................................3
5. Open Command................................................................................................................................4
6. With statement.................................................................................................................................5
7. Writing data to a csv file................................................................................................................5
8. Read data from CSV file.................................................................................................................5
9. Close Command................................................................................................................................5
1. What is CSV File
CSV (Comma Separated Values) is a simple file format, extensively used to
store tabular data, such as a spreadsheet or database. A CSV file (Comma
Separated Value) arrange tabular data. is a type of text file with ASCII or
UNICODE characters that uses specific structuring to separate values. CSV file
can be defined as a delimited text file that uses a comma (or other character)

2. Structure of a CSV File


In CSV files
 Comma separated Values.
 Comma or the character used for separation is called a delimiter.
 Csv module is used for file manipulation.
 No special character used to identify csv files. The name indicates the same.

 Each line present in the file is referred as Record and every line has the
same structure.
 Each record consists of a fixed number of values called Fields.
 Each field is separated by some specified character (or sequence of
characters), typically a comma (hence the name "comma separated
values"). The character (or characters) that separates the fields is called
a Delimiter.
 Typically, the default delimiter of CSV file is the comma, but modern
implementation of CSV file allows different delimiter character other
than comma.
 Files in the CSV format can be imported to and exported from programs
that store data in tables, such as Microsoft Excel or OpenOffice Calc and
can be opened in any text editor like Notepad.

3. Advantages of CSV File
 CSV files are very easy to manipulate programmatically.
 CSV is human readable and easy to edit manually.
 CSV files are smaller, faster to handle and easy to generate.
 CSV file can store large amount of data.
 CSV is simple to implement and parse.
 CSV file can be processed by almost all existing applications.
 CSV files are designed in such a way that complex data from one
application can be exported to a CSV file, and then import the data in
that CSV file into another application. The resulting data is human-
readable and can be easily viewed with a text editor like Notepad or a
spreadsheet program like Microsoft Excel.

4. CSV File manipulation in Python


CSV file manipulation follows the steps
 Open the CSV file
 Writing data to the file or Reading from the file
 Closing the file
5. Open Command
fd = open (file_name , mode)
open() Parameters

 file_name - File name should have the extension .csv


 mode (optional) - mode while opening a file.
 newline = "", as different operating system use different characters for
EOL, “” ensures that csv file created in one operating system can be
used in another.
Available file modes are:

Mod Description
e
'r' Open a file for reading.
'r+' Open a file for reading+.
'w' Open a file for writing.
Creates a new file if it does not exist or truncates the file if it exists.
'w+' Open a file for writing+.
Creates a new file if it does not exist or truncates the file if it exists.
'a' Open a file for exclusive creation.
If the file already exists, the operation fails.
'a+' Open a file for appending+
If the file already exists, the operation fails.

 Return Value from open()


The open() function returns a file object which can used to read, write and

modify the file. If the file is not found, it raises


the FileNotFoundError exception.

6. With statement
with open(file_name, mode, newline = string) as file_object:

import csv
with open("emp.csv","w",newline=””) as fd #CSV file opened
wtr = csv.writer(fd) #writer object wtr created
wtr.writerow([“Rohit”, 35, “Bombay”], [“Hardhik”, 30, “Gujarat”] , [“Rishabh”, 29, “Delhi”])
7. Writing data to a csv file

import csv
fd = open("emp.csv","w",newline=””) #CSV file opened
wtr = csv.writer(fd) #writer object wtr created
wtr.writerow(["Empno", "Name", "Salary"]) # Heading Record
wtr.writerow([“Rohit Sharma” , 35 , “Bombay”])
wtr.writerow([“Hardhik Pandya” , 30 , “Gujarat”])
wtr.writerow([“Rishabh Pant” , 29 , “Delhi”])
OR
wtr.writerows([[“Rohit Sharma”, 35, “Bombay”], [“Hardhik Pandya”, 30, “Gujarat”] , [“Rishabh Pant”, 29,
“Delhi”]])
fd.close() #csv file closed

wtr = csv.writer(fd,delimiter=’|’) #writer object wtr created


wtr.writerow(["Empno", "Name", "Salary"]) # Heading Record
8. Read data from CSV file.

Records = csv.reader(fd,delimiter=’|’)

import csv
fd = open("emp.csv","r",newline=””) #CSV file opened
Records = csv.reader(fd)
for rec in Records:
print(rec)
fd.close() #csv file closed

This will display all the records of the file.


9. Notes:
1. To update any record from the file
 Open the file in read mode
 Read all the records and store the records in a list.
 Update the list for the required changes.
 Close the file
 Open the file again in write mode.
 Write all the records from the list in the file.
 Close the file.
2. To add any record from the file a given position
 Open the file in read mode
 Read all the records and store the records in a list.
 Add the record in the list at the required position.
 Close the file
 Open the file again in write mode.
 Write all the records from the list in the file.
 Close the file.
3. To delete any record from the file from a given position
 Open the file in read mode
 Read all the records and store the records in a list.
 Remove the record in the list from the required
position.
 Close the file
 Open the file again in write mode.
 Write all the records from the list in the file.
 Close the file.

You might also like