Completed
CSV Files - csv module
Questions From This Challenge
Q1mcq1 mark
Which module provides functions to read and write CSV (Comma Separated Values) files in Python?
pickle
csv ✓
os
json
Q2mcq1 mark
Which function creates a writer object that can be used to write rows to a CSV file?
csv.reader()
csv.writer() ✓
csv.DictWriter() is the only option
csv.load()
Q3mcq1 mark
Which method of a csv writer object writes a single row (given as a list) to the CSV file?
writerow() ✓
writerows()
writeline()
write()
Q4mcq1 mark
Which method of a csv writer object writes multiple rows at once, given as a list of lists?
writerow()
writerows() ✓
writemany()
append()
Q5mcq1 mark
In what way should a CSV file typically be opened for writing with the csv module to avoid extra blank lines appearing between rows?
open(filename, "w", newline="") ✓
open(filename, "wb")
open(filename, "a+")
open(filename, "rt")
Q6mcq1 mark
What does csv.reader(f) return when iterated over?
Each row as a single comma-separated string
Each row as a list of string values ✓
Each row as a dictionary
The entire file content as one string
Q7mcq1 mark
Which of these is a valid way to write the header row ["Name", "Age"] to an open file object f using the csv module?
writer = csv.writer(f); writer.writerow(["Name", "Age"]) ✓
csv.write(f, ["Name", "Age"])
f.writerow(["Name", "Age"])
writer = csv.writer(f); writer.write(["Name", "Age"])
Q8output1 mark
What is the output?
import csv
with open("c1.csv", "w", newline="") as f:
writer = csv.writer(f)
writer.writerow(["Name", "Marks"])
writer.writerow(["Ravi", 88])
with open("c1.csv", "r", newline="") as f:
reader = csv.reader(f)
for row in reader:
print(row)
Correct output:
['Name', 'Marks']
['Ravi', '88']Q9output1 mark
What is the output?
import csv
data = [["Amit", 72], ["Sneha", 95], ["Kabir", 60]]
with open("c2.csv", "w", newline="") as f:
writer = csv.writer(f)
writer.writerows(data)
with open("c2.csv", "r", newline="") as f:
reader = csv.reader(f)
count = 0
for row in reader:
count = count + 1
print(count)
Correct output:
3Q10output1 mark
What is the output?
import csv
with open("c3.csv", "w", newline="") as f:
writer = csv.writer(f)
writer.writerow(["Item", "Price"])
writer.writerow(["Pen", 10])
writer.writerow(["Book", 50])
with open("c3.csv", "r", newline="") as f:
reader = csv.reader(f)
next(reader)
total = 0
for row in reader:
total = total + int(row[1])
print(total)
Correct output:
60Q11output1 mark
What is the output?
import csv
def addRecord(filename, row):
with open(filename, "a", newline="") as f:
writer = csv.writer(f)
writer.writerow(row)
with open("c4.csv", "w", newline="") as f:
writer = csv.writer(f)
writer.writerow(["Roll", "Name"])
addRecord("c4.csv", [1, "Neha"])
addRecord("c4.csv", [2, "Omar"])
with open("c4.csv", "r", newline="") as f:
reader = csv.reader(f)
for row in reader:
print(row)
Correct output:
['Roll', 'Name']
['1', 'Neha']
['2', 'Omar']Q12output1 mark
What is the output?
import csv
with open("c5.csv", "w", newline="") as f:
writer = csv.writer(f, delimiter="|")
writer.writerow(["A", "B", "C"])
with open("c5.csv", "r", newline="") as f:
content = f.read()
print(content.strip())
Correct output:
A|B|CQ13output1 mark
What is the output?
import csv
with open("c6.csv", "w", newline="") as f:
writer = csv.writer(f)
writer.writerows([["Rahul", "90"], ["Priya", "85"], ["Sara", "78"]])
with open("c6.csv", "r", newline="") as f:
reader = csv.reader(f)
rows = list(reader)
print(rows[1][0])
Correct output:
PriyaQ14output1 mark
What is the output?
import csv
try:
with open("missing_data.csv", "r", newline="") as f:
reader = csv.reader(f)
for row in reader:
print(row)
except FileNotFoundError:
print("CSV file missing")
Correct output:
CSV file missingQ15output1 mark
What is the output?
import csv
names = ["Ira", "Vikram", "Tanya"]
scores = [67, 89, 45]
with open("c8.csv", "w", newline="") as f:
writer = csv.writer(f)
writer.writerow(["Name", "Score"])
i = 0
while i < len(names):
writer.writerow([names[i], scores[i]])
i = i + 1
with open("c8.csv", "r", newline="") as f:
reader = csv.reader(f)
next(reader)
for row in reader:
if int(row[1]) >= 60:
print(row[0])
Correct output:
Ira
Vikram