Computer Science
Identify the error in the following code.
import csv
f = open('attendees1.csv')
csv_f = csv.reader()
for row in csv_f:
print(row)
Python File Handling
3 Likes
Answer
import csv
f = open('attendees1.csv') #error 1
csv_f = csv.reader() #error 2
for row in csv_f:
print(row)
- To use the
csv.reader()
function, the file should be opened in read mode ('r'). - The reader object should be in syntax
<name-of-reader-object> = csv.reader(<file-handle>)
.
The corrected code is :
import csv
f = open('attendees1.csv', 'r')
csv_f = csv.reader(f)
for row in csv_f:
print(row)
Answered By
2 Likes
Related Questions
What is the output of the following considering the file data.csv given below.
File data.csv contains: Identifier;First name;Last name 901242;Riya;Verma 207074;Laura;Grey 408129;Ali;Baig 934600;Manit;Kaur 507916;Jiva;Jain
import csv with open('C:\data.csv', 'r+') as f: data = csv.reader(f) for row in data: if 'the' in row : print(row)
Identify the error in the following code.
import csv f = open('attendees1.csv') csv_f = csv.writer(f)
Identify the error in the following code.
import pickle data = ['one', 2, [3, 4, 5]] with open('data.dat', 'wb' : pickle.dump(data)
Write a program that reads a text file and creates another file that is identical except that every sequence of consecutive blank spaces is replaced by a single space.