KEMBAR78
File Handling CSV | PDF | Comma Separated Values | Computing
0% found this document useful (0 votes)
15 views9 pages

File Handling CSV

The document provides an overview of handling CSV (Comma Separated Values) files in Python, including their structure, opening and closing procedures, and methods for reading and writing data. It emphasizes the importance of the newline argument to avoid end-of-line translation issues across different operating systems. Additionally, it details the use of the csv module's reader and writer objects for efficient data manipulation in CSV format.

Uploaded by

sohinee445
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)
15 views9 pages

File Handling CSV

The document provides an overview of handling CSV (Comma Separated Values) files in Python, including their structure, opening and closing procedures, and methods for reading and writing data. It emphasizes the importance of the newline argument to avoid end-of-line translation issues across different operating systems. Additionally, it details the use of the csv module's reader and writer objects for efficient data manipulation in CSV format.

Uploaded by

sohinee445
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/ 9

CBSE Class 12 Computer Science: File Handling - Working with

CSV Files

Contents
1 Introduction to CSV Files 2

2 Opening and Closing CSV Files 2


2.1 Role of the newline Argument in Opening CSV Files . . . . . . . . . . . . . . . . . . . . . 3

3 Writing in CSV Files 3

4 Reading in CSV Files 6

y
op
tc
no
Do

1
1 Introduction to CSV Files
CSV (Comma Separated Values) files are a common and widely used format for storing tabular data.
Tabular data refers to data organized in rows and columns, like what you would see in spreadsheets or
databases. In a CSV file, each line typically represents a data record, and within each record, individual
values (or fields) are separated by a specific character, most commonly a comma (,). While the comma
is the default and most popular delimiter, modern implementations of CSV files are flexible and allow
you to choose a different delimiter character. Other popular delimiters include the tab (\t), colon (:),
pipe (—), and semi-colon (;) characters. The separator character used in a CSV file is generally referred
to as a delimiter.
Since CSV files are essentially text files, you could technically apply standard text file procedures
to them (reading lines and then splitting values using the split() function). However, Python offers a
much more robust and convenient way to handle CSV files through its built-in csv module. This module
provides specialized functionalities to read and write tabular data in CSV format, abstracting away the
complexities of parsing delimiters and handling various data types. The csv module simplifies working
with delimited sequences as records in a CSV file by providing two specific types of objects:
• Reader objects: Used for reading data from CSV files.
• Writer objects: Used for writing data to CSV files.
To use the csv module in your Python program, you must first import it:
1 import c s v
CSV files are popular for several reasons:
• They are easy to create and understand.
y
• They are a preferred export and import format for databases and spreadsheets due to their
op

simple, universal structure.


tc

• They are capable of storing large amounts of data efficiently.


no

2 Opening and Closing CSV Files


Do

Opening a CSV file in Python is similar to opening any other text file. However, there are two important
considerations to ensure proper handling:
• File Extension: It is a convention to specify the file extension as .csv (e.g., student.csv).
• newline Argument: This is an optional but crucial argument in the open() function, especially
when working with CSV files, to handle newline characters correctly.
Basic Syntax for Opening a CSV File:
Dfile = open(”stu.csv”, ”w”) # CSV file opened in write mode
# Or
File1 = open(”stu.csv”, ”r”) # CSV file opened in read mode
Closing CSV Files: An open CSV file is closed in the same manner as any other file, using the close()
method associated with its file handle:
Dfile.close()
Important Notes on File Modes for CSV Files:
• Like text files, a CSV file will be created automatically if it does not exist and is opened in an
output-oriented file mode (”w”, ”w+”, ”a”, ”a+”).
• If the file already exists:
– The ”w” and ”w+” modes will overwrite the existing contents of the file.
– The ”a” and ”a+” modes will retain the existing contents and append new data to the end
of the file.

2
2.1 Role of the newline Argument in Opening CSV Files
The newline argument in the open() function plays a significant role in how Python handles newline
characters when working with CSV files. This is particularly important because different operating
systems use different characters or sequences of characters to designate the End-of-Line (EOL).

Table 1: EOL Characters Used in Different Operating Systems

Symbol/Character Meaning Operating System


\r CR (Carriage Return) Macintosh
\n LF (Line Feed) UNIX
\r\n CR/LF (Carriage Return/Line Feed) MS-DOS, Windows, OS/2
\0 Null character Other OSs

The Problem of EOL Translation: When you create a text file (including CSV files) on one
operating system and then attempt to read it on another, the EOL characters might be misinterpreted.
For example, if a CSV file created on a Windows OS (using \r\n as EOL) is read on a UNIX system
(which expects \n), the \r character might be treated as part of the data rather than an EOL marker.
Conversely, if you have a \r character as part of your text string (not intended as an EOL) and open
the file on a Macintosh system, the Mac OS might incorrectly treat every \r as an EOL, leading to
unexpected line breaks.
The Solution: Suppressing EOL Translation with newline=” To prevent these EOL translation
issues and ensure consistent behavior across different platforms, you should specify the newline argument
as an empty string (”) – no space in between the quotes – when opening CSV files. Syntax with newline
Argument:
y
Dfile = open(”stu.csv”, ”w”, newline=’’)
op

# CSV file opened in write mode with no EOL translation


# Or
tc

File1 = open(”stu.csv”, ”r”, newline=’’)


# CSV file opened in read mode with no EOL translation
no

By setting newline=” (a null string), you instruct Python to suppress the EOL translation. This ensures
Do

that the CSV file is created or read without any additional characters being inserted or misinterpreted
due to operating system differences. This practice is beneficial not only for cross-platform compatibility
but also for consistent behavior even when working on the same platform, as it prevents blank rows from
appearing when reading files that were written without newline=” (a common issue discussed later).

3 Writing in CSV Files


Writing data into CSV files involves converting your Python data (typically in the form of lists or tuples)
into a delimited string format suitable for the CSV structure, and then storing it in the file. The csv
module provides specialized functions for this purpose. The three primary functions used for writing to
CSV files are:
• csv.writer(): This function returns a writer object. This object is responsible for converting
the user’s data into the CSV-writable, delimited string form according to the CSV settings (e.g.,
delimiter character).

• writerobject.writerow(): This method writes one row of data (a sequence like a list or tuple)
onto the writer object. The writer object then handles the conversion and writes the delimited
string to the linked CSV file on disk.
• writerobject.writerows(): This method writes multiple rows of data (a nested sequence, like a
list of lists or a list of tuples) onto the writer object in one go. Each inner sequence is treated as a
separate row.
Steps to Write onto a CSV File:
1. Import csv module: import csv

3
Figure 1: Role of the csv.writer object (textual description)

• User Data: Input from the user (e.g., via keyboard) or existing Python data.
• csv.writerow() / csv.writerows(): This function is used to write the user data (as a Python se-
quence) to the csv.writer object.
• csv.writer object (in Memory): This object is the key intermediary. It takes the Python se-
quence, converts it into the CSV-writable delimited string format (e.g., by inserting commas be-
tween values), as per the CSV settings (like the chosen delimiter).
• Delimited Data: The converted, delimited data is then streamed out.
• CSV file on storage disk: The delimited data is written to the actual CSV file on the disk.

2. Open CSV file: Open the CSV file in a write mode (”w”, ”a”, etc.) using a file handle. For
optimal behavior, it’s highly recommended to use newline=” here (e.g., fh = open(”student.csv”,
”w”, newline=”)).

3. Create the writer object: Use csv.writer() to create a writer object, linking it to your file
handle.
Syntax: name-of-writer-object = csv.writer(file-handle, [delimiter = delimiter character])
If you omit the delimiter argument, the default delimiter (comma ,) will be used.
Example (default delimiter): stuwriter = csv.writer(fh)
Example (custom delimiter): stuwriter = csv.writer(fh, delimiter=’—’) (This will create a file with
y
pipe — as the delimiter).
op

4. Prepare user data: Obtain user data and organize it into a Python sequence (typically a list or
tc

a tuple) that represents a single row.


Example: sturec = [11, ”Neelam”, 79.0]
no

5. Write the data: Use writerow() for single rows or writerows() for multiple rows.
Do

• writerow(): stuwriter.writerow(sturec)
• writerows(): This function takes a nested sequence (e.g., a list of lists) where each inner list
is a row.
Example: Sturec multiple = [[11, ”Nistha”, 79.0], [12, ”Rudy”, 89.0]]
stuwriter.writerows(Sturec multiple)
You can also write column headings (header rows) in the same way as any other row: stuwriter.writerow([’Rollno’,
’Name’, ’Marks’])
6. Close the file: After writing all data, close the file using fh.close().

You only need to write data to the writer object; the writer object itself handles the conversion to
delimited form and writing to the linked CSV file.
Example 1: Creating a CSV file (Student.csv) and writing 5 student records
This program demonstrates writing a header row and then iteratively collecting student data from the
user to write 5 records into Student.csv using writerow().

1 import c s v
2

3 # Open t h e CSV f i l e i n w r i t e mode .


4 # For t h i s example , n e w l i n e = ’ ’ i s o m i t t e d t o d e m o n s t r a t e EOL t r a n s l a t i o n i s s u e s
later .
5 f h = open ( ” Student . c s v ” , ”w” )
6 # Create a w r i t e r o b j e c t l i n k e d to the f i l e handle
7 s t u w r i t e r = csv . w ri te r ( fh )
8

9 # Write t h e h e a d e r row
10 s t u w r i t e r . w r i t e r o w ( [ ’ R o l l n o ’ , ’Name ’ , ’ Marks ’ ] )

4
11

12 # Loop t o g e t and w r i t e 5 s t u d e n t r e c o r d s
13 f o r i in range (5) :
14 p r i n t ( f ” Student r e c o r d – i +1˝” )
15 r o l l n o = i n t ( i n p u t ( ” Enter r o l l n o : ” ) )
16 name = i n p u t ( ” Enter name : ” )
17 marks = f l o a t ( i n p u t ( ” Enter marks : ” ) )
18

19 # Create a l i s t r e p r e s e n t i n g the current student record


20 s t u r e c = [ r o l l n o , name , marks ]
21 # Write t h e s t u d e n t r e c o r d t o t h e CSV f i l e
22 s t u w r i t e r . writerow ( s t u r e c )
23

24 # Close the f i l e
25 fh . c l o s e ()
26 p r i n t ( ” Student . c s v c r e a t e d . ” )

Listing 1: Writing 5 Student Records to Student.csv

Sample Run Output:


Student record 1
Enter rollno:11
Enter name: Nistha
Enter marks:79
Student record 2
Enter rollno:12
Enter name: Rudy
y
op

Enter marks:89
Student record 3
tc

Enter rollno:13
Enter name: Rustom
no

Enter marks: 75
Student record 4
Do

Enter rollno:14
Enter name: Gurjot
Enter marks:89
Student record 5
Enter rollno:15
Enter name: Sadaf
Enter marks:85
Student.csv created.
After running this program, a file named Student.csv will be created. If you open it with a text editor,
it will look like this:

Rollno,Name,Marks
11,Nistha,79.0
12,Rudy,89.0
13,Rustom,75.0
14,Gurjot,89.0
15,Sadaf,85.0
Example 2: Writing multiple rows using writerows()
This program demonstrates writing a complete set of data to compresult.csv using writerows().

1 import c s v
2

3 # Open t h e CSV f i l e i n w r i t e mode ( n e w l i n e = ’ ’ o m i t t e d f o r demo )


4 f h = open ( ” c o m p r e s u l t . c s v ” , ”w” )
5 c w r i t e r = csv . w ri te r ( fh )
6

5
7 # Data f o r t h e c o m p e t i t i v e programming c o m p e t i t i o n , i n c l u d i n g h e a d e r
8 compdata = [
9 [ ’Name ’ , ’ P o i n t s ’ , ’ Rank ’ ] ,
10 [ ’ Shradha ’ , 4 5 0 0 , 2 3 ] ,
11 [ ’ Nishchay ’ , 4 8 0 0 , 3 1 ] ,
12 [ ’ Ali ’ , 4500 , 2 5 ] ,
13 [ ’ Adi ’ , 5 1 0 0 , 1 4 ]
14 ]
15

16 # Write a l l rows from t h e n e s t e d l i s t t o t h e CSV f i l e


17 c w r i t e r . w r i t e r o w s ( compdata )
18

19 # Close the f i l e
20 fh . c l o s e ()
21 print ( ” compresult . csv created . ” )

Listing 2: Writing Multiple Rows with writerows()

After running this program, compresult.csv will contain:

Name,Points,Rank
Shradha,4500,23
Nishchay,4800,31
Ali,4500,25
Adi,5100,14

4 Reading in CSV Files


y
op

Reading from a CSV file involves loading its data, parsing it (removing delimiters), and then making it
tc

available in a Python iterable (like a list of lists) from which you can fetch rows one by one. The primary
function for reading from CSV files is:
no

• csv.reader(): This function returns a reader object. This object loads data from the CSV file,
parses it (removes the delimiters), and presents the data in the form of a Python iterable. From
Do

this iterable, you can fetch one row of data at a time, typically using a for loop.

Figure 2: Role of the csv.reader object (textual description)

• CSV file on storage disk: The source of the delimited data.

• csv.reader object (in Memory): This object reads the delimited data from the CSV file, parses
it (removes the delimiters), and loads it into an iterable structure in memory.
• Loop for reading: A for loop is used to fetch one row of data at a time from the reader object.
• Iterable (in Memory): Each fetched row becomes an element in this iterable, ready for process-
ing.

Steps to Read from a CSV File:


1. Import csv module: import csv
2. Open CSV file: Open the CSV file in read mode (”r”) using a file handle. The file must exist,
otherwise a FileNotFoundError will be raised. It’s crucial to handle this exception.

3. Create the reader object: Use csv.reader() to create a reader object, linking it to your file
handle.
Syntax: name-of-reader-object = csv.reader(file-handle, [delimiter = delimiter character])
You can specify a delimiter if the file uses something other than a comma.

6
4. Fetch data row by row: The reader object stores the parsed data as an iterable. Fetch rows
using a for loop.
Syntax: for rec in stureader: # Process the fetched row (e.g., print(rec))
5. Process the fetched row: Each rec in the loop will be a list of strings representing the fields of
that row.
6. Close the file: After processing all data, close the file.
Recommended Practice: Using the with statement for reading The with statement is highly recom-
mended for reading CSV files. It automatically handles file opening/closing and manages exceptions.
Syntax with with statement:
1 with open ( csv - f i l e , r e a d mode , n e w l i n e= ’ ’ ) a s f i l e h a n d l e :
2 # Note n e w l i n e = ’ ’ i s c r u c i a l
3 name - o f - r e a d e r - o b j e c t = c s v . r e a d e r ( f i l e - h a n d l e )
4 f o r row i d e n t i f i e r i n r e a d e r o b j e c t :
5 # P r o c e s s t h e f e t c h e d row i n row i d e n t i f i e r

Example 1: Reading and displaying records from compresult.csv (without newline in open())
This program attempts to read compresult.csv (created where newline=” was not used during writing)
and display its contents.

1 import c s v
2

3 # Open t h e f i l e i n r e a d mode u s i n g with s t a t e m e n t


4 try :
5 with open ( ” c o m p r e s u l t . c s v ” , ” r ” ) a s f h :
6 creader = csv . reader ( fh ) # Create reader o b j e c t
y
7 # Loop t o f e t c h and p r i n t each row
op

8 for rec in creader :


9 print ( rec )
tc

10 e x c e p t FileNotFoundError :
11 p r i n t ( ” E r r o r : c o m p r e s u l t . c s v not found . ” )
no

Listing 3: Problematic Reading of compresult.csv


Do

Output (Problematic):

[’Name’, ’Points’, ’Rank’]


[]
[’Shradha’, ’4500’, ’23’]
[]
[’Nishchay’, ’4800’, ’31’]
[]
[’Ali’, ’4500’, ’25’]
[]
[’Adi’, ’5100’, ’14’]
[]

Explanation of Empty Rows: The presence of unexpected empty rows ([]) is common when reading CSV
files written without newline=” in open(). This happens due to EOL translation. On Windows, EOL is
\r\n. If csv.writer writes a row and then open() (text mode, no newline=”) adds its own \r\n, it results
in \r\n\r\n. csv.reader interprets the first \r\n as a row terminator and the second as an empty row.
The Solution:
1. Method 1: Create/Write onto CSV file with newline=” argument in open() (Preferred)
If the CSV file is created correctly by suppressing EOL translation during writing, you don’t need
to worry during reading.
Example 2: Creating Employee.csv by suppressing EOL translation (newline=”)

1 import c s v
2

3 # Open f i l e i n w r i t e mode , c r u c i a l l y s p e c i f y i n g n e w l i n e = ’ ’

7
4 f h = open ( ” Employee . c s v ” , ”w” , n e w l i n e= ’ ’ )
5 e w r i t e r = csv . w rit er ( fh )
6 empdata = [
7 [ ’Empno ’ , ’Name ’ , ’ D e s i g n a t i o n ’ , ’ S a l a r y ’ ] ,
8 [ 1 0 0 1 , ’ T r u p t i ’ , ’ Manager ’ , 5 6 0 0 0 ] ,
9 [ 1 0 0 2 , ’ Raziya ’ , ’ Manager ’ , 5 5 9 0 0 ] ,
10 [ 1 0 0 3 , ’ Simran ’ , ’ A n a l y s t ’ , 3 5 0 0 0 ] ,
11 [1004 , ’ S i l v i y a ’ , ’ Clerk ’ , 25000] ,
12 [ 1 0 0 5 , ’ S u j i ’ , ’PR O f f i c e r ’ , 3 1 0 0 0 ]
13 ]
14 e w r i t e r . w r i t e r o w s ( empdata ) # Write a l l employee data
15 p r i n t ( ” F i l e Employee . c s v s u c c e s s f u l l y c r e a t e d with no EOL t r a n s l a t i o n . ” )
16 fh . c l o s e ()

Listing 4: Creating Employee.csv with newline

If you open Employee.csv in Notepad, it will look clean, without extra blank lines.
Example 3: Reading and displaying Employee.csv (created with newline=”)
This program reads Employee.csv. Notice newline argument is not typically needed here for reading
if written correctly with newline=”.

1 import c s v
2

3 # Open t h e f i l e i n r e a d mode u s i n g with s t a t e m e n t


4 try :
5 # Using n e w l i n e = ’ ’ f o r r e a d i n g i s a l s o a good p r a c t i c e f o r c o n s i s t e n c y
6 with open ( ” Employee . c s v ” , ” r ” , n e w l i n e= ’ ’ ) a s f h :
y
7 ereader = csv . reader ( fh ) # Create reader o b j e c t
op

8 p r i n t ( ” F i l e Employee . c s v c o n t a i n s : ” )
9 f o r r e c i n e r e a d e r : # Loop t o f e t c h and p r i n t each r e c o r d
tc

10 print ( rec )
11 e x c e p t FileNotFoundError :
no

12 p r i n t ( ” E r r o r : Employee . c s v not found . ” )


Do

Listing 5: Reading Correctly Written Employee.csv

Output:

File Employee.csv contains:


[’Empno’, ’Name’, ’Designation’, ’Salary’]
[’1001’, ’Trupti’, ’Manager’, ’56000’]
[’1002’, ’Raziya’, ’Manager’, ’55900’]
[’1003’, ’Simran’, ’Analyst’, ’35000’]
[’1004’, ’Silviya’, ’Clerk’, ’25000’]
[’1005’, ’Suji’, ’PR Officer’, ’31000’]

As expected, no blank lines.

2. Method 2: To read from a file created with EOL translation (if you can’t control
writing)
If reading a CSV created without newline=”, tell open() what EOL to expect.
Syntax: open(csvfilename, readmode, newline=’EOL character of OS’)
For example, if created on Windows, use newline=’\r\n’.
Example 4: Reading compresult.csv by specifying newline=’\r\n’
Modifies earlier problematic reading for compresult.csv.

1 import c s v
2

3 # Open f i l e , e x p l i c i t l y t e l l i n g i t t o t r e a t ’\ r \n ’ a s n e w l i n e
4 try :
5 with open ( ” c o m p r e s u l t . c s v ” , ” r ” , n e w l i n e= ’ \ r \n ’ ) a s f h :

8
6 creader = csv . reader ( fh )
7 for rec in creader :
8 print ( rec )
9 e x c e p t FileNotFoundError :
10 p r i n t ( ” E r r o r : c o m p r e s u l t . c s v not found . ” )

Listing 6: Reading compresult.csv with Specific newline

Output:

[’Name’, ’Points’, ’Rank’]


[’Shradha’, ’4500’, ’23’]
[’Nishchay’, ’4800’, ’31’]
[’Ali’, ’4500’, ’25’]
[’Adi’, ’5100’, ’14’]

No blank lines, as newline=’\r\n’ correctly interprets the \r\n sequence.

y
op
tc
no
Do

You might also like