Computer Science
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)
Answer
This code will produce no output.
Explanation
By default, csv.reader()
uses a comma (,) as the delimiter to separate values in a CSV file. But the delimiter in the file data.csv
is semicolon (;), hence the rows won't split correctly, leading to each row being treated as a single string. When the code checks if the row contains the word 'the', it will only print rows where 'the' appears in the entire row. Therefore, the given code will not output anything.
Related Questions
What will be the output of the following code ?
import pickle ID = {1:"Ziva", 2:"53050", 3:"IT", 4:"38", 5:"Dunzo"} fin = open("Emp.pkl","wb") pickle.dump(ID, fin) fin.close() fout = open("Emp.pkl",'rb') ID = pickle.load(fout) print(ID[5])
What will be the output of the following code ?
import pickle List1 = ['Roza', {'a': 23, 'b': True}, (1, 2, 3), [['dogs', 'cats'], None]] List2 = ['Rita', {'x': 45, 'y': False}, (9, 5, 3), [['insects', 'bees'], None]] with open('data.pkl', 'wb') as f: f.write(List1) with open('data.pkl', 'wb') as f: f.write(List2) with open('data.pkl', 'rb') as f: List1 = pickle.load(f) print(List1)
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 csv f = open('attendees1.csv') csv_f = csv.reader() for row in csv_f: print(row)