12th Standard Syllabus & Materials
12th Standard
TN 12th English Poem - 6 - Incident of the French Camp Sample Question Papers Study Material - QB365 Set A
NEW12th Standard
TN 12th English Prose - 6 - On the Rule of the Road Sample Question Papers Study Material - QB365 Set A
NEW12th Standard
TN 12th English Prose - 5 - The Chair Sample Question Papers Study Material - QB365 Set A
NEW12th Standard
TN 12th English Supplementary - 4 - The Midnight Visitor Sample Question Papers Study Material - QB365 Set A
NEW12th Standard
TN 12th English Poem - 4 - Ulysses Sample Question Papers Study Material - QB365 Set A
NEW12th Standard
TN 12th English Prose - 4 - The Summit Sample Question Papers Study Material - QB365 Set A

Published on: 06/01/2020
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.
Fields containing line breaks denoted by
CSV
XLS
CLRF
CRLF
2.
Which of the following can protect if the data itself contains commas in CSV file?
' '
, ,
" "
3.
4.
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
5.
The command used to skip a row in a CSV file is
next()
skip()
omit()
bounce()
6.
What are the line terminator accepted by python CSV module?
7.
How will you read CSV file into a dictionary?
8.
What is the purpose of using 'with' statement along with open ( )?
9.
How will you sort more than one column from a csv file?Give an example statement.
10.
Mention the default modes of the File.
11.
Write a program to create a new normal CSV file to store data.
12.
What are the ways to write a new or edit are existing CSV file in python?
13.
How will you save the CSV file in MS-Excel?
14.
What is the difference between the write mode and append mode.
15.
Write a Python program to modify an existing file.
16.
How will you sort more than one column in a CSV file? Explain with an example.
17.
Write a program to read a specific column in a CSV file.
18.
Write the rules to be followed to format the data in a CSV file.
19.
Tabulate the different mode with its meaning.
1.
(d)
CRLF
2.
(c)
" "
3.
(c)
4.
(a)
Line Terminator
5.
(a)
next()
6.
\r and \n are the line terminator accepted by Python CSV module.
7.
(i) To read a CSV file into a dictionary can be done by using DictReader class of csv module which works similar to the reader ( )class but creates an object which maps data to a dictionary.
(ii) The keys are given by the fieldnames as parameter.
8.
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.
9.
To sort by more than one column you can use itemgetter with multiple indices:
operator.itemgetter
Example:
sortedlist = sorted (data, key = operator. itemgetter(1))
10.
The default is reading in text mode. In this mode, while reading from the file the data would be in the format of strings.
11.
Import csv
csvData = [['Student, 'Age'], [Dhanush', '17'], ['Kalyani', '18'], ['Ram', '15']]
with open('c:\\pyprg\\ch13\\Pupil.csv', 'w') as CF:
writer = csv.writer(CF) # CF is the file object
writer.writerows(csvData) # csvData is the List name
CF.close( )
12.
(i) Creating A New Normal CSV File
(ii) Modifying An Existing File
(iii) Writing on a CSV File with Quotes
(iv) Writing on a CSV File with Custom Delimiters
(v) Writing on a CSV File with Lineterminator
(vi) Writing on a CSV File with Quotechars
(vii) Writing CSV File Into A Dictionary
(viii) Getting Data At Runtime And Writing In a File
13.
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:
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.
14.
Write mode:
Open a file for writing. Creates a new file if it does not exist or truncates the file if it exists.
Append mode:
Open for appending at the end of the file without truncating it. Creates a new file if it does not exist.
15.
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 |
16.
To sort by more than one column you can use itemgetter with multiple indices: operator. itemgetter (1,2).
The following program do the task mentioned above using operator.itemgetter(col_no)
Example:
# 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']
17.
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
18.
Rules to be followed to format data in a CSV file:
(i) 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
(ii) The last record in the file mayor may not have an ending line break.
For example:
ppp, qqq↲
yyy, xxx
(iii) 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_name 1, field_name 2, field_name 3↲
aaa,bbb,ccc↲
zzz, yyy, xxx CRLF(Carriage Return and Line Feed)
(iv) 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
(v) Each field mayor 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"↲
Black, White, Yellow
(vi) Fields containing line breaks (CRLF), double quotes, and commas should be enclosed in double-quotes.
For example:
Red, ",", Blue CRLF
Red, Blue, Green
(vii) 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",
White
19.
| Python file modes | 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. |
| '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) |
12th Standard Syllabus & Materials
12th Standard
TN 12th English Supplementary - 3 - The Hour of Truth (Play) Sample Question Papers Study Material - QB365 Set A
NEW12th Standard
TN 12th English Poem - 3 - All the World’s a Stage Sample Question Papers Study Material - QB365 Set A
NEW12th Standard
TN 12th English Prose - 3 - In Celebration of Being Alive Sample Question Papers Study Material - QB365 Set A
NEW12th Standard
TN 12th English Supplementary - 2 - Life of Pi 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