12th Standard Syllabus & Materials
12th Standard
TN 12th Computer Applications மின்னணு தரவு பரிமாற்றம் Sample Question Papers Study Material - QB365 Set A
NEW12th Standard
TN 12th Computer Applications மின் - வணிக பாதுகாப்பு அமைப்புகள் Sample Question Papers Study Material - QB365 Set A
NEW12th Standard
TN 12th Computer Applications மின்னணு செலுத்தல் முறைகள் Sample Question Papers Study Material - QB365 Set A
NEW12th Standard
TN 12th Computer Applications மின் - வணிகம் Sample Question Papers Study Material - QB365 Set A
NEW12th Standard
TN 12th Computer Applications திறந்த மூல கருத்துருக்கள் Sample Question Papers Study Material - QB365 Set A
NEW12th Standard
TN 12th Computer Applications வலையமைப்பு வடமிடல் Sample Question Papers Study Material - QB365 Set A

Published on: 31/10/2019
Python and CSV Files
Download Tamil Nadu 12th Standard Computer Science question papers, model tests, one-mark questions, important questions, and public exam papers in PDF format. Free study materials and answer keys for TN State Board students.
Questions + Answers key
Take MCQ Computer Science Test

1.
2.
Which of the following creates an object which maps data to a dictionary?
listreader()
reader()
tuplereader()
DictReader ()
3.
Which of the following is a string used to terminate lines produced by writer()method of csv module?
Line Terminator
Enter key
Form feed
Data Terminator
4.
Which of the following module is provided by Python to do several operations on the CSV files?
py
xls
csv
os
5.
A CSV file is also known as a ….
Flat File
3D File
String File
Random File
6.
What is the use of CSV file?
7.
Why CSV file is known as flat file?
8.
What is use of next() function?
9.
Mention the default modes of the File.
10.
What is CSV File?
11.
Write the syntax of reader( ).
12.
How will you create CSV normal file?
13.
What is the difference between reader() and DictReader() function?
14.
Write a Python program to modify an existing file.
15.
Write a note on open() function of python. What is the difference between the two methods?
16.
Write a program to read a specific column in a CSV file.
17.
Write a Python program to write a CSV File with custom quotes.
18.
Write the different methods to read a File in Python.
19.
Differentiate Excel file and CSV file.
1.
(c)
2.
(d)
DictReader ()
3.
(a)
Line Terminator
4.
(c)
csv
5.
(a)
Flat File
6.
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
7.
A CSV is known as flat file because files in the CSV format can be imported to and exported from programs that store data in tables, such as Microsoft Excel or OpenOfficeCalc
8.
The row heading is get sorted, to avoid the first row should be skipped. This is can be done by using the command "next( )".
9.
The default is reading in text mode. In this mode, while reading from the file the data would be in the format of strings.
10.
A CSV file is a human readable text file where each line has a number of fields, separated by commas or some other delimiter.
11.
'The syntax for csv.reader() is
csv.reader(fileobject,delimiter,fmtparams)
where
(i) file object: passes the path and the mode of the file.
(ii) delimiter: an optional parameter containing the standard dilects like , | etc can be omitted
(iii) fmtparams: optional parameter which help to override the default values of the dialects like skipinitialspace, quoting etc. Can be omitted.
12.
To create a CSV file in Notepad,
(i) First open a new file using
File ⟶New or ctrl +N.
(ii) Then enter the data separating each value with a comma and each row with a new line.
(iii) For example consider the following details
Topic1, Topic2, Topic3
one, two, three
Example1, Example2, Example3
(iv) Save this content in a file with the extension.csv.
13.
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.
14.
Program: To modify an existing file
import csv
row = ['3: 'Meena','Bangalore']
with open('Student.csv', 'r') as read file:
reader = csv.reader (read File)
lines = list (reader)
lines [3] = row
with open ('Student.csv', 'w') as write file:
# 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()
Output:
| Roll No | Name | City |
| 1. | Harshini, | Chennai |
| 2. | Adhith, | Mumbai |
| 3. | Meena, | Bangalore |
| 4. | Egiste | Tiruchy |
| 5. | Venkat, | Madurai |
15.
(i) 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 # f is file object
>>> f = open ('c:\\Pyprg\ch13 sample5.csv') # specifying full path
(ii) The default is reading in text mode. In this mode, while reading from the file the data would be in the format of strings.
(iii) 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.
16.
Import csv
#opening the csv file which is in different location with read mode
f=open("c:\\pyprg\\ch13ample5.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
17.
We can write the csv file with quotes, by registering new dialects using csv.register_dialect() class of csv module.
Program:
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') as f:
writer = csv.writer(f, dialect='myDialect')
for row in info:
writer. writerow(row)
f.close()
Output
file: "Person.csv"
"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".
18.
1. CSV file - data with default delimiter comma(,)
2. CSV file - data with space at the beginning.
3. CSV file - data with quotes
4. CSV file - data with custom Delimiters
1. CSV file - data with default delimiter comma(,):
The following program read a file called "Sample1.csv" with default delimiter comma (,) and print row by row.
Program:
#importing csv
import csv
#opening the csv file which is in different location with read mode
with opent('c.\\pyprg\\sample1.csv', 'r') 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
print(row)
F.close()
Output:
['SNO', 'NAME', 'CITY']
['12101', 'RAM', 'CHENNAI']
['12102', 'LAVANYA', 'TIRUCHY']
['12103', 'LAKSHMAN', 'MADURAI']
2. CSV files - data with spaces at the beginning:
Consider the following file "Sample2.csv containing the following data when opened through notepad.
| Topic 1 | Topic 2 | Topic 3 |
| One, | two, | three, |
| Example 1, | Example 2, | Example 3, |
Program:
import csv
csv.register_dialect ['my Dialect', delimiters=', 'Skipinitialspace= True).
F = open ('c:\\Pyprgl\sample2.csv', 'r')
reader = CSV.reader (F, dialect = 'myDialect')
for row in reader:
print (row)
F.close()
Output:
['Topic 1', 'Topic 2', 'Topic 3']
['One', 'two', 'three']
['Example 1', 'Example 2', 'Example 3']
We can see in "Sample 2.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.
3. CSV File - Data with Quotes:
We can read the csv files with quotes, by registering new dialects using csv. register_dialect( ) class of csv module.
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."
Program:
import cSv
csv.register_ dialect ('MyDialect', delimiter =, 'quoting = csv.QUOTE_ALL, 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':]
4. CSV files with custom Delimiters:
We can read csv file having custom the 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 |
| 103 | Ayush | M |
| 104 | Abinandh | M |
Program:
import csv
Csv. register_dialect('myDialect', delimiter= '|')
with open (c:\\pyprg\sample 4.csv', 'r') as f:
reader = csv.reader (f, dialect = "my dialect")
for row is reader:
print (row)
f.close()
Output:
['Roll no', 'Name', 'City]
['12101', 'Arun', 'Chennai']
['12102', 'Meena', 'Kovai']
['12103', 'Ram', 'Nellai']
19.
| Excel | CSV |
|---|---|
| Excel is a binary file that holds information about all the worksheets in a file, including both content and formatting | CSV format is a plain text format with a series of values separated by commas. |
| XLS files can only be read by applications that have been especially written to read their format, and can only be written in the same way. | CSV can be opened with any text editor in Windows like notepad, MS Excel, Open Office, etc. |
| Excel is a spreadsheet that saves files into its own proprietary format viz. xls or xlsx | CSV is a format for saving tabular information into a delimited text file with extension .csv |
| Excel consumes-more memory while importing data | Importing CSV files can be much faster, and it also consumes less memory |
12th Standard Syllabus & Materials
12th Standard
TN 12th Computer Applications களப்பெயர் முறைமை (DNS) Sample Question Papers Study Material - QB365 Set A
NEW12th Standard
TN 12th Computer Applications வலையமைப்பு எடுத்துக்காட்டுகள் மற்றும் நெறிமுறைகள் Sample Question Papers Study Material - QB365 Set A
NEW12th Standard
TN 12th Computer Applications கணினி வலையமைப்பு ஓர் அறிமுகம் Sample Question Papers Study Material - QB365 Set A
NEW12th Standard
TN 12th Computer Applications PHP-உடன் MySQL-ஐ இணைத்தல் Sample Question Papers Study Material - QB365 Set A
Tamilnadu Stateboard 12th Standard Subjects

Maths

Chemistry

Physics

Biology

Computer Science

Business Maths and Statistics

Economics

Commerce

Accountancy

History

Computer Applications

Biology

Computer Technology

Computer Applications

Computer Science

Business Maths and Statistics

Commerce

Economics

Maths

Chemistry

Physics

Computer Technology

History

Accountancy

Tamil

English

French
Tamilnadu Stateboard Standards