Importing C++ Programs

Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

SELECT Gender, count(*) FROM Student GROUP BY Gender;

Gender count(*)
M 5
F 3

Note
The * is used with the COUNT to include the NULL values.

The GROUP BY applies the aggregate functions independently to a series of groups that
are defined by having a field value in common. The output of the above SELECT statement
gives a count of the number of Male and Female students.

12.7.5.8 HAVING clause


The HAVING clause can be used along with GROUP BY clause in the SELECT statement
to place condition on groups and can include aggregate functions on them. For example to
count the number of students belonging to chennai.
SELECT Place , count(*) FROM Student GROUP BY Place HAVING place = ‘Chennai’;

Place count
Chennai 2
The above output shows the no. of students belongs to chennai.

12.7.6 TCL commands


12.7.6.1 COMMIT command
The COMMIT command is used to permanently save any transaction to the database.
When any DML commands like INSERT, UPDATE, DELETE commands are used, the
changes made by these commands are not permanent. It is marked permanent only after the
COMMIT command is given from the SQL prompt. Once the COMMIT command is given,
the changes made cannot be rolled back. The COMMIT command is used as

COMMIT;

12.7.6.2 ROLLBACK command


The ROLLBACK command restores the database to the last commited state. It is used
with SAVEPOINT command to jump to a particular savepoint location. The syntax for the
ROLLBACK command is :
ROLL BACK TO save point name;

219 Structured Query Language

12th Computer Science_EM Chapter 12.indd 219 22-12-2022 11:54:47


12.7.6.3 SAVEPOINT command
The SAVEPOINT command is used to temporarily save a transaction so that you
can rollback to the point whenever required. The different states of our table can be saved at
anytime using different names and the rollback to that state can be done using the ROLLBACK
command.
SAVEPOINT savepoint_name;

Example showing COMMIT, SAVEPOINT and ROLLBACK in the student table


having the following data:

Admno Name Gender Age Place


105 Revathi F 19 Chennai
106 Devika F 19 Bangalore
103 Ayush M 18 Delhi
101 Adarsh M 18 Delhi
104 Abinandh M 18 Chennai

INSERT INTO Student VALUES (107, 'Beena', 'F', 20 , 'Cochin');


COMMIT;

Admno Name Gender Age Place


105 Revathi F 19 Chennai
106 Devika F 19 Bangalore
103 Ayush M 18 Delhi
101 Adarsh M 18 Delhi
104 Abinandh M 18 Chennai
107 Beena F 20 Cochin

UPDATE Student SET Name = ‘Mini’ WHERE Admno=105;


SAVEPOINT A;

Admno Name Gender Age Place


105 Mini F 19 Chennai
106 Devika F 19 Bangalore
103 Ayush M 18 Delhi
101 Adarsh M 18 Delhi
104 Abinandh M 18 Chennai
107 Beena F 20 Cochin

XII Std Computer Science 220

12th Computer Science_EM Chapter 12.indd 220 22-12-2022 11:54:47


INSERT INTO Student VALUES(108, 'Jisha', 'F', 19, 'Delhi');
SAVEPOINT B;

Admno Name Gender Age Place


105 Mini F 19 Chennai
106 Devika F 19 Bangalore
103 Ayush M 18 Delhi
101 Adarsh M 18 Delhi
104 vAbinandh M 18 Chennai
107 Beena F 20 Cochin
108 Jisha F 19 Delhi

ROLLBACK TO A;

Admno Name Gender Age Place


105 Mini F 19 Chennai
106 Devika F 19 Bangalore
103 Ayush M 18 Delhi
101 Adarsh M 18 Delhi
104 Abinandh M 18 Chennai
107 Beena F 20 Cochin

Points to remember:
• SQL is a language that helps to create and operate relational databases.
• MySQL is a database management system.
• The various components of SQL are Data Definition Language (DDL), Data
Manipulation Language (DML), Data Query Language (DQL), Transactional
Control Language (TCL), Data Control Language (DCL).
• The DDL provides statements for creation and deletion of tables.
• The DML provides statements to insert, update and delete data of a table.
• The DCL provides authorization commands to access data.
• The TCL commands are used to manage transactions in a database.
• The DQL commands help to generate queries in a database.
• The CREATE TABLE command creates a new table.

221 Structured Query Language

12th Computer Science_EM Chapter 12.indd 221 22-12-2022 11:54:47


Hands on Experience
1. Create a query of the student table in the following order of fields name, age, place and
admno.
2. Create a query to display the student table with students of age more than 18 with unique
city.
3. Create a employee table with the following fields employee number, employee name,
designation, date of joining and basic pay.
4. In the above table set the employee number as primary key and check for NULL values in
any field.
5. Prepare a list of all employees who are Managers.

Evaluation

Part - I

Choose the best answer (1 Mark)


1. Which commands provide definitions for creating table structure, deleting relations, and
modifying relation schemas.
a. DDL b. DML c. DCL d. DQL
2. Which command lets to change the structure of the table?
a. SELECT b. ORDER BY c. MODIFY d. ALTER
3. The command to delete a table including the structure is
a. DROP b. DELETE c. DELETE ALL d. ALTER TABLE
4. Queries can be generated using
a. SELECT b. ORDER BY c. MODIFY d. ALTER
5. The clause used to sort data in a database
a. SORT BY b. ORDER BY c. GROUP BY d. SELECT

Part -II

Answer the following questions (2 Marks)


1. Write a query that selects all students whose age is less than 18 in order wise.
2. Differentiate Unique and Primary Key constraint.
3. Write the difference between table constraint and column constraint?
4. Which component of SQL lets insert values in tables and which lets to create a table?
5. What is the difference between SQL and MySQL?

XII Std Computer Science 222

12th Computer Science_EM Chapter 12.indd 222 22-12-2022 11:54:47


Part -III

Answer the following questions (3 Marks)


1. What is a constraint? Write short note on Primary key constraint.
2. Write a SQL statement to modify the student table structure by adding a new field.
3. Write any three DDL commands.
4. Write the use of Savepoint command with an example.
5. Write a SQL statement using DISTINCT keyword.

Part -IV

Answer the following questions (5 Marks)


1. Write the different types of constraints and their functions.
2. Consider the following employee table. Write SQL commands for the qtns.(i) to (v).

EMP CODE NAME DESIG PAY ALLO WANCE

S1001 Hariharan Supervisor 29000 12000


P1002 Shaji Operator 10000 5500
P1003 Prasad Operator 12000 6500
C1004 Manjima Clerk 8000 4500
M1005 Ratheesh Mechanic 20000 7000

(i) To display the details of all employees in descending order of pay.


(ii) To display all employees whose allowance is between 5000 and 7000.
(iii) To remove the employees who are mechanic.
(iv) To add a new row.
(v) To display the details of all employees who are operators.
3. What are the components of SQL? Write the commands in each.
4. Construct the following SQL statements in the student table-
(i) SELECT statement using GROUP BY clause.
(ii) SELECT statement using ORDER BY clause.
5. Write a SQL statement to create a table for employee having any five fields and create a
table constraint for the employee table.

223 Structured Query Language

12th Computer Science_EM Chapter 12.indd 223 22-12-2022 11:54:47


CHAPTER 13
Unit IV
PYTHON AND CSV FILES

Learning Objectives

After the completion of this chapter, the student will be able to


• Understand what is CSV?
• Able to import CSV files in python programs
• Execute and debug python programs

13.1 Introduction

Python has a vast library of modules that are included with its distribution. One among
the module is the CSV module which gives the Python programmer the ability to parse CSV
(Comma Separated Values) files. A CSV file is a human readable text file where each line
has a number of fields, separated by commas or some other delimiter. You can assume each
line as a row and each field as a column. The CSV module will be able to read and write the vast
majority of CSV files.
13.2 Difference between CSV and XLS file formats

The difference between Comma-Separated Values (CSV) and eXceL Sheets(XLS) file
formats is
Excel CSV
Excel is a binary file that holds information CSV format is a plain text format with a
about all the worksheets in a file, including series of values separated by commas.
both content and formatting
XLS files can only be read by applications CSV can be opened with any text editor
that have been especially written to read their in Windows like notepad, MS Excel,
format, and can only be written in the same OpenOffice, etc.
way.
Excel is a spreadsheet that saves files into its CSV is a format for saving tabular
own proprietary format viz. xls or xlsx information into a delimited text file with
extension .csv
Excel consumes more memory while Importing CSV files can be much faster, and
importing data it also consumes less memory

XII Std Computer Science 224

12th Computer Science_EM Chapter 13.indd 224 23-12-2022 15:39:36


Files saved in excel cannot be opened or edited by text editors.

13.3 Purpose Of CSV File

CSV is a simple file format used to store tabular data, such as a spreadsheet or database.
Since they're plain text, they're easier to import into a spreadsheet or another storage database,
regardless of the specific software you're using.
You can open CSV files in a spreadsheet program like Microsoft Excel or in a text editor
or through a database which make them easier to read.

Note
CSV File cannot store charts or graphs. It stores data but does not contain
formatting, formulas, macros, etc.

A CSV file is also known as a Flat File. Files in the CSV format can be
imported to and exported from programs that store data in tables, such as Microsoft
Excel or OpenOfficeCalc

13.4 Creating a CSV file using Notepad (or any text editor)

A CSV file is a text file, so it can be created and edited using any text editor, But more
frequently a CSV file is created by exporting a spreadsheet or database in the program that
created it.

13.4.1 Creating CSV Normal File


To create a CSV file in Notepad, First open a new file using
File →New or ctrl +N.
Then enter the data you want the file to contain, separating each value with a comma
and each row with a new line.
For example consider the following details
Topic1,Topic2,Topic3
one,two,three
Example1,Example2,Example3

Save this content in a file with the extension .csv . You can then open the same using
Microsoft Excel or any other spreadsheet program. Here we have opened using Microsoft
Excel. It would create a table of data similar to the following:

225 Python and CSV Files

12th Computer Science_EM Chapter 13.indd 225 23-12-2022 15:39:36


Topic1 Topic2 Topic3

one two three


Example1 Example2 Example3

Fig. 13.4.1 CSV file when opened in MS-Excel

In the above CSV file, you can observe the fields of data were separated by commas. But
what happens if the data itself contains commas in it?
If the fields of data in your CSV file contain commas, you can protect them by enclosing
those data fields in double-quotes (“). The commas that are part of your data will then be kept
separate from the commas which delimit the fields themselves.

13.4.2 Creating CSV File That contains Comma With Data


For example, let’s say that one of our fields contain commas in the description. If our
data looked like the below example:

RollNo Name Address


12101 Nivetha Mylapore, Chennai
12102 Lavanya Adyar, Chennai
12103 Ram Gopalapuram, Chennai

To retain the commas in “Address” column, you can enclose the fields in quotation
marks. For example:
RollNo, Name, Address
12101, Nivetha, “Mylapore, Chennai”
12102, Lavanya, “Adyar, Chennai”
12103, Ram, “Gopalapuram, Chennai”
As you can see, only the fields that contain commas are enclosed in quotes. If you open
this in MS Excel, It looks like as follows

XII Std Computer Science 226

12th Computer Science_EM Chapter 13.indd 226 23-12-2022 15:39:36


Roll No Name Address
12101 Nivetha Mylapore, Chennai
12102 Lavanya Adyar, Chennai

12103 Ram Gopalapuram, Chennai

Fig 13.4.2 (a) CSV Field data with comma in Excel

The same goes for newlines (display data in more than one line example Address
column) which may be part of your field data. Any fields containing a newline as part of its
data need to be enclosed in double-quotes.
For Example
RollNo Name Address
Mylapore,
12101 Nivetha
Chennai
Adyar,
12102 Lavanya
Chennai
Gopalapuram,
12103 Ram
Chennai

It should be written in CSV file as

RollNo, Name, Address


12101, Nivetha, “Mylapore
Chennai”
12102, Lavanya, “Adyar
Chennai”
12103, Ram, “Gopalapuram
Chennai”

227 Python and CSV Files

12th Computer Science_EM Chapter 13.indd 227 23-12-2022 15:39:37


The Result will look like this

Roll No Name Address


Mylapore,
12101 Nivetha Chennai
12102 Lavanya Adyar,
Chennai
12103 Ram Gopalapuram,
Chennai

Fig 13.4.2 (b) CSV Field Data with newline in Excel

13.4.3 Creating CSV File That contains Double Quotes With Data
If your fields contain double-quotes as part of their data, the internal quotation
marks need to be doubled so that they can be interpreted correctly. For Example, given the
following data:

Roll No Name Favorite Sports Address


12101 Nivetha “Cricket”, “Football” Mylapore Chennai
12102 Lavanya “Basketball”, “Cricket” Adyar Chennai
12103 Ram “Soccer”, “Hockey” Gopala puram Chennai

It should be written in csv file as

RollNo, Name, FavoriteSports, Address


12101, Nivetha,””” Cricket ””,”” Football ”””, Mylapore chennai
12102, Lavanya,””” Basketball ””,”” Cricket ”””, Adyar chennai
12103, Ram,””” Soccer””,”” Hockey”””, Gopalapuram chennai

The output will be

XII Std Computer Science 228

12th Computer Science_EM Chapter 13.indd 228 23-12-2022 15:39:37


Roll No Name Favorite Sports Address
12101 Nivetha "Cricket", "Football" Mylapore, Chennai
12102 Lavanya "Basketball", "Cricket" Adyar, Chennai
12103 Ram "Soccer", "Hockey" Gopalapuram, Chennai

Fig 13.4.3 CSV Field Data with Double quotes in Excel


13.4.4 Rules to be followed to format data in a CSV file
1. Each record (row of data) is to be located on a separate line, delimited by a line break by
pressing enter key. For example:
xxx,yyy
denotes enter Key to be pressed

2. The last record in the file may or may not have an ending line break. For example:

ppp, qqq
yyy, xxx

3. There may be an optional header line appearing as the first line of the file with the same
format as normal record lines. The header will contain names corresponding to the fields
in the file and should contain the same number of fields as the records in the rest of the
file. For example:

field_name1,field_name2,field_name3
aaa,bbb,ccc
zzz,yyy,xxx CRLF( Carriage Return and Line Feed)

229 Python and CSV Files

12th Computer Science_EM Chapter 13.indd 229 23-12-2022 15:39:37


4. Within the header and each record, there may be one or more fields, separated by commas.
Spaces are considered part of a field and should not be ignored. The last field in the record
must not be followed by a comma. For example: Red , Blue
5. Each field may or may not be enclosed in double quotes. If fields are not enclosed with
double quotes, then double quotes may not appear inside the fields. For example:

"Red","Blue","Green" #Field data with doule quotes


Black,White,Yellow #Field data without doule quotes

6. Fields containing line breaks (CRLF), double quotes, and commas should be enclosed in
double-quotes. For example:

Red, “,”, Blue CRLF # comma itself is a field value.so it is enclosed with double quotes
Red, Blue , Green

7. If double-quotes are used to enclose fields, then a double-quote appearing inside a field
must be preceded with another double quote. For example:

““Red””, ““Blue””, ““Green”” CRLF # since double quotes is a field value it is enclosed with another
double quotes
, , White

Note
The last row in the above example (, , White ) begins with two commas because the first
two fields of that row were empty in our spreadsheet. Don't delete them — the two commas
are required so that the fields correspond from row to row. They cannot be omitted.

13.5 Create A CSV File Using Microsoft Excel

To create a CSV file using Microsoft Excel, launch Excel and then open the file you
want to save in CSV format. For example, below is the data contained in our sample Excel
worksheet:

XII Std Computer Science 230

12th Computer Science_EM Chapter 13.indd 230 23-12-2022 15:39:37


Item Name Cost - Rs Quantity Profit
Keyboard 480 12 1152
Monitor 5200 10 10400
Mouse 200 50 2000
Total Profit 13552

Fig 13.5 Sample Worksheet Data


Once the data is entered in the worksheet, select File → Save As option, and for the
“Save as type option”, select CSV (Comma delimited) or type the file name along with extension
.csv.
Saving excel file as CSV

Fig 13.6 Save As dialog box


After you save the file, you are free to open it up in a text editor to view it or to edit it
manually. Its contents will resemble the following:

231 Python and CSV Files

12th Computer Science_EM Chapter 13.indd 231 23-12-2022 15:39:37


Item Name, Cost-Rs, Quantity, Profit
Keyboard, 480, 12, 1152
Monitor, 5200, 10, 10400
Mouse, 200, 50, 2000
,,Total Profit =,13552

13.5.1 Microsoft Excel to open a CSV file


If Microsoft Excel has been installed on the computer, by default CSV files should
open automatically in Excel when the file is double-clicked. If you are getting an Open With
prompt when opening the CSV file, choose Microsoft Excel from the available programs to
open the file.
Alternatively, you can open Microsoft Excel and in the menu bar, select File → Open,
and select the CSV file. If the file is not listed, make sure to change the file type to be opened
to Text Files (*.prn, *.txt, *.csv).

If both MS Excel and Open Office calc is installed in the computer, by


default the CSV file will be opened in MS Excel.

13.6 Read and write a CSV file Using Python

Python provides a module named CSV, using this you can do several operations on the
CSV files. The CSV library contains objects and other code to read, write, and process data
from and to CSV files.

CSV files have been used extensively in e-commerce applications because


they are considered very easy to process.

13.6.1 Read a CSV File Using Python


There are two ways to read a CSV file.
1. Use the csv module’s reader function
2. Use the DictReader class.
Two ways of Reading CSV File

reader () function Dict Reader class

Fig 13.7 Ways to read CSV file

XII Std Computer Science 232

12th Computer Science_EM Chapter 13.indd 232 23-12-2022 15:39:37


When you want to read from or write to a file ,you need to open it. Once the reading
is over it needs to be closed. So that, resources that are tied with the file are freed. Hence, in
Python, a file operation takes place in the following order

Step 1 Open a file

Step 2 Perform Read or write operation

Step 3 Close the file

Note
File name or the complete path name can be represented either with in “ “ or in ‘ ‘
in the open command.

Python has a built-in function open() to open a file. This function returns a file
object, also called a handle, as it is used to read or modify the file accordingly.

For Example

>>> f = open("sample.txt") # open file in current directory and f is file object


>>> f = open('c:\pyprg\ch13sample5.csv') # specifying full path

You can specify the mode while opening a file. In mode, you can specify whether you
want to read 'r', write 'w' or append 'a' to the file. you can also specify “text or binary” in which
the file is to be opened.

The default is reading in text mode. In this mode, while reading from the file the data
would be in the format of strings.

On the other hand, binary mode returns bytes and this is the mode to be used when
dealing with non-text files like image or exe files.

Python File Modes


Mode Description
'r' Open a file for reading. (default)
'w' Open a file for writing. Creates a new file if it does not exist or truncates the file if
it exists.
'x' Open a file for exclusive creation. If the file already exists, the operation fails.

233 Python and CSV Files

12th Computer Science_EM Chapter 13.indd 233 23-12-2022 15:39:37


'a' Open for appending at the end of the file without truncating it. Creates a new file
if it does not exist.
't' Open in text mode. (default)
'b' Open in binary mode.
'+' Open a file for updating (reading and writing)

f=open("sample.txt")
#equivalent to 'r' or 'rt'
f = open("sample.txt",'w') # write in text mode
f = open("image1.bmp",'r+b') # read and write in binary mode
Python has a garbage collector to clean up unreferenced objects but, one must not
rely on it to close the file.

f = open("test.txt") # since no mode is specified the default mode rt is used


# perform file operations
f.close()

The above method is not entirely safe. If an exception occurs when you are performing
some operation with the file, the code exits without closing the file. The best way to do this is
using the “with” statement. This ensures that the file is closed when the block inside with is
exited. You need not to explicitly call the close() method. It is done internally.

with open("test.txt",’r’) as f:
# f is file object to perform file operations

Closing a file will free up the resources that were tied with the file and is done using
Python close() method.
f = open("sample.txt")
# perform file operations
f.close()

13.6.1.1 CSV Module’s Reader Function


You can read the contents of CSV file with the help of csv.reader() function. The reader
function is designed to take each line of the file and make a list of all columns. Then, you
just choose the column you want the variable data for. Using this function one can read data
from csv files of different formats like quotes (" "), pipe (|) and comma (,).
The syntax for csv.reader() is

XII Std Computer Science 234

12th Computer Science_EM Chapter 13.indd 234 23-12-2022 15:39:37


csv.reader(fileobject,delimiter,fmtparams)
where
file object :- passes the path and the mode of the file
delimiter :- an optional parameter containing the standard dilects like , | etc can be omitted
fmtparams: optional parameter which help to override the default values of the dialects like
skipinitialspace,quoting etc. Can be omitted

CSV file - data with default delimiter comma (,)


1

CSV file - data with Space at the beginning


2

CSV file - data with quotes


3

CSV file - data with custom Delimiters


4

13.6.1.1.1 CSV file with default delimiter comma (,)


The following program read a file called “sample1.csv” with default delimiter comma (,)
and print row by row.
#importing csv
import csv
#opening the csv file which is in different location with read mode
with open('c:\pyprg\sample1.csv', 'r', newline‘=’) as F:
#other way to open the file is f= ('c:\pyprg\sample1.csv', 'r')
reader = csv.reader(F)
# printing each line of the Data row by row
for row in reader:
print(row)
F.close()
OUTPUT
['SNO', 'NAME', 'CITY']
['12101', 'RAM', 'CHENNAI']
['12102', 'LAVANYA', 'TIRUCHY']
['12103', 'LAKSHMAN', 'MADURAI']

13.6.1.1.2. CSV files- data with Spaces at the beginning


Consider the following file “sample2.csv” containing the following data when opened
through notepad
Topic1, Topic2, Topic3,
one, two, three
Example1, Example2, Example3

235 Python and CSV Files

12th Computer Science_EM Chapter 13.indd 235 23-12-2022 15:39:37


The following program read the file through Python using “csv.reader()”.

import csv
csv.register_dialect('myDialect',delimiter = ',',skipinitialspace=True)
F=open('c:\pyprg\sample2.csv','r')
reader = csv.reader(F, dialect='myDialect')
for row in reader:
print(row)
F.close()

OUTPUT
['Topic1', 'Topic2', 'Topic3']
['one', 'two', 'three']
['Example1', 'Example2', 'Example3']

As you can see in “sample2.csv” there are spaces after the delimiter due to which the
output is also displayed with spaces.

These whitespaces can be removed, by registering new dialects using csv.register_dialect()


class of csv module. A dialect describes the format of the csv file that is to be read. In dialects
the parameter “skipinitialspace” is used for removing whitespaces after the delimiter.

Note
By default “skipinitialspace” has a value false

The following program reads “sample2.csv” file, which contains spaces after the delimiter.

import csv
csv.register_dialect('myDialect',delimiter = ',',skipinitialspace=True)
F=open('c:\pyprg\sample2.csv','r')
reader = csv.reader(F, dialect='myDialect')
for row in reader:
print(row)
F.close()
OUTPUT
['Topic1', 'Topic2', 'Topic3']
['one', 'two', 'three']
['Example1', 'Example2', 'Example3']

XII Std Computer Science 236

12th Computer Science_EM Chapter 13.indd 236 23-12-2022 15:39:37


Note
A dialect is a class of csv module which helps to define parameters for
reading and writing CSV. It allows you to create, store, and re-use various formatting
parameters for your data.

13.6.1.1.3 CSV File-Data With Quotes


You can read the csv file with quotes, by registering new dialects using csv.register_dialect()
class of csv module.
Here, we have quotes.csv file with following data.

SNO,Quotes
1, "The secret to getting ahead is getting started."
2, "Excellence is a continuous process and not an accident."
3, "Work hard dream big never give up and believe yourself."
4, "Failure is the opportunity to begin again more intelligently."
5, "The successful warrior is the average man, with laser-like focus."

The following Program read “quotes.csv” file, where delimiter is comma (,) but the
quotes are within quotes (“ “).
import csv
csv.register_dialect('myDialect',delimiter = ',',skipinitialspace=True)
f=open('c:\pyprg\quotes.csv','r')
reader = csv.reader(f, dialect='myDialect')
for row in reader:
print(row)

OUTPUT
['SNO', 'Quotes']
['1', 'The secret to getting ahead is getting started.']
['2', 'Excellence is a continuous process and not an accident.']
['3', 'Work hard dream big never give up and believe yourself.']
['4', 'Failure is the opportunity to begin again more intelligently.']
['5', 'The successful warrior is the average man, with laser-like focus. ']

In the above program, register a dialect with name myDialect. Then, we used csv.
QUOTE_ALL to display all the characters after double quotes.

237 Python and CSV Files

12th Computer Science_EM Chapter 13.indd 237 23-12-2022 15:39:37


13.6.1.1.4 CSV files with Custom Delimiters
You can read CSV file having custom delimiter by registering a new dialect with the
help of csv.register_dialect().
In the following file called “sample4.csv”,each column is separated with | (Pipe symbol)

Roll No | Name | City


12101 | Arun | Chennai
12102 | Meena | Kovai
12103 | Ram | Nellai

The following program read the file “sample4.csv” with user defined delimiter “|”

import csv
csv.register_dialect('myDialect', delimiter = '|') OUTPUT
with open('c:\pyprg\sample4.csv', 'r', newline‘=’) as f: ['RollNo', 'Name', 'City']
reader = csv.reader(f, dialect='myDialect') ['12101', 'Arun', 'Chennai']
for row in reader: ['12102', 'Meena', 'Kovai']
print(row) ['12103', 'Ram', 'Nellai']
f.close()

In the above program, a new dialects called myDialect is registered. Use the delimiter=|
where a pipe (|) is considered as column separator.

13.6.2 Read a specific column In a File


To get the specific columns like only Item Name and profit for the “sample5.csv” file .
Then you have to do the following:

import csv
#opening the csv file which is in different location with read mode
f=open("c:\pyprg\ch13sample5.csv",'r')
#reading the File with the help of csv.reader()
readFile=csv.reader(f)
#printing the selected column
for col in readFile :
print (col[0],col[3])
f.close()
sample5.csv File in Excel

XII Std Computer Science 238

12th Computer Science_EM Chapter 13.indd 238 23-12-2022 15:39:37


A B C D

1 item Nam Cost-Rs Quantity Profit


2 Keyboard 480 12 1152
3 Monitor 5200 10 10400
4 Mouse 200 50 2000

sample5.csv File with selected col

OUTPUT
Item Name Profit
Keyboard 1152
Monitor 10400
Mouse 2000

13.6.3 Read A CSV File And Store It In A List


In this topic you are going to read a CSV file and the contents of the file will be stored
as a list. The syntax for storing in the List is

list = [] # Start as the empty list


list.append(element) # Use append() to add elements

For example all the row values of “sample.csv” file is stored in a list using the following
program

import csv
# other way of declaring the filename
inFile= 'c:\pyprg\sample.csv'
F=open(inFile,'r')
reader = csv.reader(F)
# declaring array
arrayValue = []
# displaying the content of the list
for row in reader:
arrayValue.append(row)
print(row)
F.close()
sample.csv opened in MS-Excel

239 Python and CSV Files

12th Computer Science_EM Chapter 13.indd 239 23-12-2022 15:39:37


A1 fx Topic 1

>
A B C
1 Topic 1 Topic 2 Topic 3
2 One two three
3 Example 1 Example 2 Example 3
4
OUTPUT
['Topic1', 'Topic2', 'Topic3']
[' one', 'two', 'three']
['Example1', 'Example2', 'Example3']

Note
A list is a data structure in Python that is a mutable, or changeable,
ordered sequence of elements.

List literals are written within square brackets [ ]. Lists work similarly to strings

13.6.4 Read A CSV File And Store A Column Value In A List For Sorting

In this program you are going to read a selected column from the “sample6.csv” file by
getting from the user the column number and store the content in a list.

XII Std Computer Science 240

12th Computer Science_EM Chapter 13.indd 240 23-12-2022 15:39:37


Item Name Cost - Rs Quantity Profit
Keyboard 480 12 1152
Monitor 5200 10 10400
Mouse 200 50 2000

Fig 13.6.4 CSV file Data for a selected column for sorting

Since the row heading is also get sorted, to avoid that the first row should be skipped.
This is can be done by using the command “next()”. The list is sorted and displayed.

# sort a selected column given by user leaving the header column in


# descending order of value
import csv
# other way of declaring the filename
inFile= ‘c:\pyprg\sample6.csv’
# opening the csv file which is in the same location of where the current #
python file

241 Python and CSV Files

12th Computer Science_EM Chapter 13.indd 241 23-12-2022 15:39:37


F=open(inFile,’r’)
# reading the File with the help of csv.reader()
reader = csv.reader(F)
# skipping the first row(heading)
next(reader)
# declaring a list
arrayValue = []
a = int(input (“Enter the column number between 0 to 3:-“))
# sorting a particular column-cost
for row in reader:
arrayValue.append(row[a])
arrayValue.sort(reverse=True)
for row in arrayValue:
print (row)
F.close()

OUTPUT
Enter the column number between 0 to 3:- 2
50
12
10

Read a specific column in a csv file and display its result in Ascending
order

list_name.sort() command arranges a list value in ascending order. list_name.


sort(reverse=True) is used to arrange a list in descending order

13.6.5 Sorting A CSV File With A Specified Column


In this program you are going to see the “sample8.csv” file’s entire content is transferred
to a list. Then the list of rows is sorted and displayed in ascending order of quantity. To sort
by more than one column you can use itemgetter with multiple indices: operator .itemgetter
(1,2), The content of “sample8.csv” is

XII Std Computer Science 242

12th Computer Science_EM Chapter 13.indd 242 23-12-2022 15:39:37


sample8.csv in Excel screen

Item Name Quantity


Keyboard 48
Monitor 52
Mouse 20

sample8.csv in Notepad
ItemName ,Quantity
Keyboard, 48
Monitor,52
Mouse ,20

Fig 13.6.5 CSV file Data into a list for sorting

The following program do the task mentioned above using operator.itemgetter(col_no)


#Program to sort the entire row by using a specified column.
# declaring multiple header files
import csv ,operator
#One more way to read the file
data = csv.reader(open(‘c:\pyprg\sample8.csv’))
next(data) #(to omit the header)
#using operator module for sorting multiple columns
sortedlist = sorted (data, key=operator.itemgetter(1)) # 1 specifies we want to sort
# according to second column
for row in sortedlist:
print(row)
OUTPUT
[‘Mouse ‘, ‘20’]
[‘Keyboard ‘, ‘48’]
[‘Monitor’, ‘52’]

243 Python and CSV Files

12th Computer Science_EM Chapter 13.indd 243 23-12-2022 15:39:38


Note
The sorted() method sorts the elements of a given item in a specific order –
Ascending or Descending. Sort() method which performs the same way as sorted().
Only difference, sort() method doesn’t return any value and changes the original list
itself.

Add one more column “cost” in “sample8.csv” and sort it in descending order
of cost by using the syntax
sortedlist = sorted(data, key=operator.itemgetter(Col_number),reverse=True)

13.6.6 Reading CSV File Into A Dictionary


To read a CSV file into a dictionary can be done by using DictReader method of csv
module which works similar to the reader() class but creates an object which maps data to a
dictionary. The keys are given by the fieldnames as parameter. DictReader works by reading
the first line of the CSV and using each comma separated value in this line as a dictionary key.
The columns in each subsequent row then behave like dictionary values and can be accessed
with the appropriate key (i.e. fieldname).
If the first row of your CSV does not contain your column names, you can pass a
fieldnames parameter into the DictReader’s constructor to assign the dictionary keys manually.
The main difference between the csv.reader() and DictReader() is in simple terms csv.
reader and csv.writer work with list/tuple, while csv.DictReader and csv.DictWriter work with
dictionary. csv.DictReader and csv.DictWriter take additional argument fieldnames that are
used as dictionary keys.
For Example Reading “sample8.csv” file into a dictionary
import csv
filename = ‘c:\pyprg\sample8.csv’
input_file =csv.DictReader(open(filename,’r’))
for row in input_file:
print(dict(row)) #dict() to print data

OUTPUT
{‘ItemName ‘: ‘Keyboard ‘, ‘Quantity’: ‘48’}
{‘ItemName ‘: ‘Monitor’, ‘Quantity’: ‘52’}
{‘ItemName ‘: ‘Mouse ‘, ‘Quantity’: ‘20’}

In the above program, DictReader() is used to read “sample8.csv” file and map into
a dictionary. Then, the function dict() is used to print the data in dictionary format without
order.
XII Std Computer Science 244

12th Computer Science_EM Chapter 13.indd 244 23-12-2022 15:39:38


Remove the dict() function from the above program and use print(row).Check
you are getting the following output
OrderedDict([(‘ItemName ‘, ‘Keyboard ‘), (‘Quantity’, ‘48’)])
OrderedDict([(‘ItemName ‘, ‘Monitor’), (‘Quantity’, ‘52’)])
OrderedDict([(‘ItemName ‘, ‘Mouse ‘), (‘Quantity’, ‘20’)])

13.6.7 Reading CSV File With User Defined Delimiter Into A Dictionary
You can also register new dialects and use it in the DictReader() methods. Suppose
“sample8.csv” is in the following format
ItemName|Quantity
Keyboard|48
Monitor|52
Mouse|20
Then “sample8.csv” can be read into a dictionary by registering a new dialect

import csv
csv.register_dialect(‘myDialect’,delimiter = ‘|’,skipinitialspace=True)
filename = ‘c:\pyprg\ch13\sample8.csv’
with open(filename, ‘r’, newline‘=’) as csvfile:
reader = csv.DictReader(csvfile, dialect=’myDialect’)
for row in reader:
print(dict(row))
csvfile.close()

OUTPUT
{‘ItemName’:‘Keyboard’,‘Quantity’: 48}
{‘ItemName’ :‘Monitor’:‘Quantity’:52}
{‘ItemName’: ‘Mouse’:‘Quantity’: 20}

Note
DictReader() gives OrderedDict by default in its output. An OrderedDict is a
dictionary subclass which saves the order in which its contents are added. To remove the
OrderedDict use dict().

13.7 Writing Data Into Different Types in Csv Files

As you know Python provides an easy way to work with CSV file and has csv module
to read and write data in the csv file. In the previous topics, You have learned how to read CSV
files in Python. In similar way, You can also write a new or edit an existing CSV files in Python.
245 Python and CSV Files

12th Computer Science_EM Chapter 13.indd 245 23-12-2022 15:39:38


Creating A New Normal CSV File
1

Modifying An Existing File


2

Writing On A CSV File with Quotes


3

Writing On A CSV File with Custom Delimiters


4

Writing On A CSV File with Lineterminator


5

Writing On A CSV File with Quotechars


6

Writing CSV File Into A Dictionary


7

Getting Data At Runtime And Writing In a File


8

13.7.1 Creating A New Normal CSV File


When you have a set of data that you would like to store inside a CSV file, it’s time to do
the opposite and use the writer function.

The csv.writer() function returns a writer object which converts the user’s data into
delimited strings on the given file-like object. The writerow() function writes a row of data
into the specified file.
The syntax for csv.writer() is

csv.writer(fileobject,delimiter,fmtparams)

where
fileobject :- passes the path and the mode of the file
delimiter :- an optional parameter containing the standard dilects like , | etc can
be omitted
fmtparams : optional parameter which help to override the default values of the
dialects like skipinitialspace,quoting etc. can be omitted

You can create a normal CSV file using writer() function of csv module having
default delimiter comma (,)
Here’s an example.
The following Python program converts a List of data to a CSV file called “Pupil.csv”
that uses, (comma) as a value separator.

XII Std Computer Science 246

12th Computer Science_EM Chapter 13.indd 246 23-12-2022 15:39:38


Import csv
csvData = [[‘Student’, ‘Age’], [‘Dhanush’, ‘17’], [‘Kalyani’, ‘18’], [‘Ram’, ‘15’]]
with open(‘c:\pyprg\ch13\Pupil.csv’, ‘w’, newline‘=’) as CF:
writer = csv.writer(CF) # CF is the file object
writer.writerows(csvData) # csvData is the List name
CF.close()

When you open the “Pupil.csv” file with a text editor, it will show the content as
follows.
Student, Age
Dhanush, 17
Kalyani, 18
Ram, 15

In the above program, csv.writer() function converts all the data in the list “csvData” to
strings and create the content as file like object. The writerows () function writes all the data in
to the new CSV file “Pupil.csv”.

Note
The writerow() function writes one row at a time. If you need to write all the data at
once you can use writerows() method.

13.7.2 Modifying An Existing File


Making some changes in the data of the existing file or adding more data is called
modification .For example the “student.csv” file contains the following data.
Roll No, Name, City
1, Harshini, Chennai
2, Adhith, Mumbai
3, Dhuruv, Bangalore
4, Krishna, Tiruchy
5, Venkat, Madurai

The following program modify the “student.csv” file by modifying the value of an
existing row in student.csv

247 Python and CSV Files

12th Computer Science_EM Chapter 13.indd 247 23-12-2022 15:39:38


import csv
row = [‘3’, ‘Meena’,’Bangalore’]
with open(‘student.csv’, ‘r’, newline‘=’) as readFile:
reader = csv.reader(readFile)
lines = list(reader) # list()- to store each row of data as a list
lines[3] = row
with open(‘student.csv’, ‘w’) as writeFile:
# returns the writer object which converts the user data with delimiter
writer = csv.writer(writeFile)
#writerows()method writes multiple rows to a csv file
writer.writerows(lines)
readFile.close()
writeFile.close()

When we open the student.csv file with text editor, then it will show:

Roll No, Name, City

1, Harshini, Chennai

2, Adhith, Mumbai

3, Meena, Bangalore

4, Krishna, Tiruchy

5, Venkat, Madurai

In the above program,the third row of “student.csv” is modified and saved. First the
“student.csv” file is read by using csv.reader() function. Then, the list() stores each row of the
file. The statement “lines[3] = row”, changed the third row of the file with the new content in
“row”. The file object writer using writerows (lines) writes the values of the list to “student.csv”
file.

13.7.2.1 ADDING NEW ROW


Sometimes, you may need to add new rows in the existing CSVfile. Adding a new row
at the end of the file is called appending a row.
The following program add a new row to the existing “student.csv” file.

XII Std Computer Science 248

12th Computer Science_EM Chapter 13.indd 248 23-12-2022 15:39:38


import csv
row = [‘6’, ‘Sajini ‘, ‘Madurai’]
with open(‘student.csv’, ‘a’, newline‘=’) as CF: # append mode to add data at the end
writer = csv.writer(CF)
writer.writerow(row) # writerow() method write a single row of data in file
CF.close()

When “student.csv” file is opened with a text editor, it displays as follows

Roll No, Name, City

1, Harshini, Chennai

2, Adhith, Mumbai

3, Meena, Bangalore

4, Krishna, Tiruchy

5, Venkat, Madurai

6, Sajini, Madurai

In the above program, a new row is appended into “student.csv”. For this, purpose only
the CSV file is opened in ‘a’ append mode. Append mode write the value of row after the last
line of the “student.csv file.”

The ‘w’ write mode creates a new file. If the file is already existing ‘w’ mode
over writs it. Where as ‘a’ append mode add the data at the end of the file if the file
already exists otherwise creates a new one

Note
writerow() takes 1-dimensional data (one row), and writerows takes 2-dimensional
data (multiple rows) to write in a file.

13.7.3 CSV Files With Quotes


You can write the csv file with quotes, by registering new dialects using
csv.register_dialect() class of csv module. The following program explains this.

249 Python and CSV Files

12th Computer Science_EM Chapter 13.indd 249 23-12-2022 15:39:38


import csv
info = [[‘SNO’, ‘Person’, ‘DOB’],
[‘1’, ‘Madhu’, ‘18/12/2001’],
[‘2’, ‘Sowmya’,’19/2/1998’],
[‘3’, ‘Sangeetha’,’20/3/1999’],
[‘4’, ‘Eshwar’, ‘21/4/2000’],
[‘5’, ‘Anand’, ‘22/5/2001’]]
csv.register_dialect(‘myDialect’,quoting=csv.QUOTE_ALL)
with open(‘c:\pyprg\ch13\person.csv’, ‘w’, newline‘=’) as f:
writer = csv.writer(f, dialect=’myDialect’)
for row in info:
writer.writerow(row)
f.close()
When you open “person.csv” file, we get following output :
“SNO”,”Person”,”DOB”
”1”,”Madhu”,”18/12/2001”
”2”,”Sowmya”,”19/2/1998”
”3”,”Sangeetha”,”20/3/1999”
”4”,”Eshwar”,”21/4/2000”
“5”,”Anand”,”22/5/2001”
In above program, a dialect named myDialect is registered(declared). Quoting=csv.
QUOTE_ALL allows to write the double quote on all the values.
13.7.4 CSV Files With Custom Delimiters
A delimiter is a string used to separate fields. The default value is comma(,).
You can have custom delimiter in CSV files by registering a new dialect with the help of
csv.register_dialect().This example Program is written using the custom delimiter pipe(|)

import csv
info = [[‘SNO’, ‘Person’, ‘DOB’],
[‘1’, ‘Madhu’, ‘18/12/2001’],
[‘2’, ‘Sowmya’,’19/2/1998’],
[‘3’, ‘Sangeetha’,’20/3/1999’],
[‘4’, ‘Eshwar’, ‘21/4/2000’],
[‘5’, ‘Anand’, ‘22/5/2001’]]
csv.register_dialect(‘myDialect’,delimiter = ‘|’)
with open(‘c:\pyprg\ch13\dob.csv’, ‘w’, newline‘=’) as f:
writer = csv.writer(f, dialect=’myDialect’)
for row in info:
writer.writerow(row)
f.close()

XII Std Computer Science 250

12th Computer Science_EM Chapter 13.indd 250 23-12-2022 15:39:38


When we open “dob.csv” file, we get the following output:

SNO|Person|DOB
1|Madhu|18/12/2001
2|Sowmya|19/2/1998
3|Sangeetha|20/3/1999
4|Eshwar|21/4/2000
5|Anand|22/5/2001
In the above program, a dialect with delimiter as pipe(|)is registered. Then the list “info”
is written into the CSV file “dob.csv”.

Note
The dialect parameter skipinitialspace when it is True, whitespace immediately following
the delimiter is ignored. The default is False.

13.7.5 CSV File With A Line Terminator


A Line Terminator is a string used to terminate lines produced by writer. The default
value is \r or \n. We can write csv file with a line terminator in Python by registering new
dialects using csv.register_dialect() class of csv module. For Example

import csv
Data = [[‘Fruit’, ‘Quantity’], [‘Apple’, ‘5’], [‘Banana’, ‘7’], [‘Mango’, ‘8’]]
csv.register_dialect(‘myDialect’, delimiter = ‘|’, lineterminator = ‘\n’)
with open(‘c:\pyprg\ch13\line.csv’, ‘w’, newline‘=’) as f:
writer = csv.writer(f, dialect=’myDialect’)
writer.writerows(Data)
f.close()

When we open the line.csv file, we get following output with spacing between lines:

Fruit|Quantity
Apple|5
Banana|7
Mango|8

In the above code, the new dialect “myDialect uses the delimiter=’|’ where a | (pipe)
is considered as column separator. The line terminator=’\r\n\r\n’ separates each row and
displays the data after one blank line in Notepad.

251 Python and CSV Files

12th Computer Science_EM Chapter 13.indd 251 23-12-2022 15:39:38


Note
Python’s CSV module only accepts \r\n, \n or \r as line terminator

13.7.6 CSV File with quote characters


You can write the CSV file with custom quote characters, by registering new dialects
using csv.register_dialect() class of csv module.

import csv
csvData = [[‘SNO’,’Items’], [‘1’,’Pen’], [‘2’,’Book’], [‘3’,’Pencil’]]
csv.register_dialect(‘myDialect’,delimiter = ‘|’,quotechar = ‘”’,
quoting=csv.QUOTE_ALL)
with open(‘c:\pyprg\ch13\quote.csv’, ‘w’, newline‘=’) as csvFile:
writer = csv.writer(csvFile, dialect=’myDialect’)
writer.writerows(csvData)
print(“writing completed”)
csvFile.close()

When you open the “quote.csv” file in notepad, we get following output:
“SNO”|“Items”
“1”|“Pen”
“2”|“Book”
“3”|“Pencil”
In the above program, myDialect uses pipe (|) as delimiter and quotechar as doublequote
‘”’ to write inside the file.
13.7.7 Writing CSV File Into A Dictionary
Using DictWriter() class of csv module, we can write a csv file into a dictionary. It
creates an object which maps data into a dictionary. The keys are given by the fieldnames
parameter. The following program helps to write the dictionary in to file.

import csv
data = [{‘MOUNTAIN’ : ‘Everest’, ‘HEIGHT’: ‘8848’},
{‘MOUNTAIN’ : ‘Anamudi ‘, ‘HEIGHT’: ‘2695’},
{‘MOUNTAIN’ : ‘Kanchenjunga’, ‘HEIGHT’: ‘8586’}]
with open(‘c:\pyprg\ch13\peak.csv’, ‘w’, newline‘=’) as CF:
fields = [‘MOUNTAIN’, ‘HEIGHT’]
w = csv.DictWriter(CF, fieldnames=fields)
w.writeheader()
w.writerows(data)
print(“writing completed”)
CF.close()

XII Std Computer Science 252

12th Computer Science_EM Chapter 13.indd 252 23-12-2022 15:39:38

You might also like