NEW
Font size
Worksheetsfile handling in python
Total questions: 25
Worksheet time: 13mins
What will be written inside the file test.csv using the following program
import csv
D = [['Exam'],['Quarterly'],['Halfyearly']]
csv.register_dialect('M',lineterminator = '\n')
with open('c:\pyprg\ch13\line2.csv', 'w') as f:
wr = csv.writer(f,dialect='M')
wr.writerows(D)
f.close()
Exam, Quarterly, Halfyearly
Exam Quarterly Halfyearly
E
Q
H
Exam
Quarterly
Halfyearly
Making some changes in the data of the existing file or adding more data is called
Editing
Appending
Modification
Alteration
What is the output of the following program? import csv d=csv.reader(open ("city.csv'))
next(d)
for row in d:
print(row)
if the file called “city.csv” contain the following details
chennai,mylapore
mumbai,andheri
chennai,mylapore
mumbai,andheri
chennai
mumbai
chennai,mylapore
mumbai,andheri
The command used to skip a row in a CSV file is
next()
skip()
omit()
bounce()
The contents of a CSV file "sample.csv" and the code to read and display the data in this file are given
What will be the values of sno, name and age (respectively) of the first record:
0,12, None
0,12, ' '
1, None, 12
1, ' ' , 12
The contents of a CSV file "sample.csv" and the code to read and display the data in this file are given
What will be the keys of rec:
0,1,2
'0', '1', '2'
'son', 'name', 'age'
'sno_0', 'name_1', 'age_2'
The contents of a CSV file "sample.csv" and the code to read and display the data in this file are given
What will be the data type of variable rec used in the code:
list
tuple
str
dict
The contents of a CSV file "sample.csv" and the code to read and display the data in this file are given
How many lines of output will this code display
2
3
4
5
The contents of the CSV file 'members.csv' are given also the code to display all the records from this file and calculate the average age of all those members whose name does not start with 'A'.
import csv
with open ("member.csv",'r','newline='') as f:
r = csv.___________________ #1
___________________ #2
s=c=0 # sum and counter
for rec in r:
if rec[1][ _ ] != 'A' : #3
s += ______________ #4
c+=1
if _______________: #5
print("Average age = ", s/c)
else: print("All names stars with 'A')
c > 0
c == 0
c >= 0
None of these
The contents of the CSV file 'members.csv' are given also the code to display all the records from this file and calculate the average age of all those members whose name does not start with 'A'.
import csv
with open ("member.csv",'r','newline='') as f:
r = csv.___________________ #1
___________________ #2
s=c=0 # sum and counter
for rec in r:
if rec[1][ _ ] != 'A' : #3
s += ______________ #4
c+=1
if _______________: #5
print("Average age = ", s/c)
else: print("All names stars with 'A')
rec[2]
int (rec[2])
rec
rec[0]
The contents of the CSV file 'members.csv' are given also the code to display all the records from this file and calculate the average age of all those members whose name does not start with 'A'.
import csv
with open ("member.csv",'r','newline='') as f:
r = csv.___________________ #1
___________________ #2
s=c=0 # sum and counter
for rec in r:
if rec[1][ _ ] != 'A' : #3
s += ______________ #4
c+=1
if _______________: #5
print("Average age = ", s/c)
else: print("All names stars with 'A')
0
1
'A'
' -1 '
The contents of the CSV file 'members.csv' are given also the code to display all the records from this file and calculate the average age of all those members whose name does not start with 'A'.
import csv
with open ("member.csv",'r','newline='') as f:
r = csv.___________________ #1
___________________ #2
s=c=0 # sum and counter
for rec in r:
if rec[1][ _ ] != 'A' : #3
s += ______________ #4
c+=1
if _______________: #5
print("Average age = ", s/c)
else: print("All names stars with 'A')
rec[1]
w [1]
first(r)
next(r)
The contents of the CSV file 'members.csv' are given also the code to display all the records from this file and calculate the average age of all those members whose name does not start with 'A'.
import csv
with open ("member.csv",'r','newline='') as f:
r = csv.___________________ #1
___________________ #2
s=c=0 # sum and counter
for rec in r:
if rec[1][ _ ] != 'A' : #3
s += ______________ #4
c+=1
if _______________: #5
print("Average age = ", s/c)
else: print("All names stars with 'A')
reader()
reader_f()
reader(f)
f.reader
The given code is leaving an unwanted blank line after every row in the file sample.csv
The correct
close the file at the end
use keyword argument newline='' with open function
make data a tuple instead of a list
use '\n' as the last element of each record
The given code is raising an exception:
To correct the code, we have to:
close the file at the end
use writerow() instead of writerows()
make row a tuple instead of a list
import pickle
The contents of a text file"sample.txt", and a script to read the file contents are shown
Pick the correct statement w.r.t the given contents and code:
rec will be list in 1 element
rec will be list in 3 element
rec will be a nested list of 1 element
rec will be a nested list of 3 elements
The contents of a csv file "sample.csv" are shown below:
sno, name, age
1, Amit, 12
2, Harsh, 13
3, Vivek, 21
It can be treated as a text file
It cannot be treated as a text file as it is a CSV file
It cannot be opened in a text editor a sit is a CSV file
An error will be raised in Python
The content of a file "sample.txt" are shown below:
1,2,3,4
'a', 'b', 'c'
5, 23, 'me', 'peace'
'to', 'be', 'or' 'not'
It can be treated as a csv file
It can not be treated as CSV file as it is a text file
It can not be treated as CSV file as it contains mixed data
It can not be treated as CSV file as it contains different number of elements in different lines.
CSV module allows writing multiple records in a CSV file using
writerows()
writerow()
writerec()
writelines
Which module in Python is used to process csv file
pickle
random
CSV
csv.reader()
File opening modes for CSV files are:
Same as those for text files
same as those for binary files
different from text files and binary files
mix of text files and binary files
We can manage (create/read/update)CSV files using
A text editor
A Python program
A Spreadsheet application
All of the above
Which character can work as delimiter for CSV files:
Comma only
Tab only
Space only
Any character
Delimeter for CSV file is
Comma
Tab
Space
Semi-colon
CSV stands for
Comma Separated Values
Comma Spaced Values
Completely Separated VAlues
Comma Segregated Values
