Importing C++ Programs
Importing C++ Programs
Importing C++ Programs
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.
Place count
Chennai 2
The above output shows the no. of students belongs to chennai.
COMMIT;
ROLLBACK TO A;
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.
Evaluation
Part - I
Part -II
Part -IV
Learning Objectives
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
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.
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:
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.
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
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
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:
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)
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.
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:
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.
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
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.
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.
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()
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.
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']
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.
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.
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
OUTPUT
Item Name Profit
Keyboard 1152
Monitor 10400
Mouse 2000
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
>
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.
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.
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
sample8.csv in Notepad
ItemName ,Quantity
Keyboard, 48
Monitor,52
Mouse ,20
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)
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
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().
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
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.
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.
The following program modify the “student.csv” file by modifying the value of an
existing row in student.csv
When we open the student.csv file with text editor, then it will show:
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.
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.
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()
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.
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.
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()