0% found this document useful (0 votes)
402 views

File Hand

Uploaded by

Titu Pandwy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
402 views

File Hand

Uploaded by

Titu Pandwy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 4
379. Diksha Behl is a student of non-IT stream. However, she wants to learn Python also as it will be helpful to her in her field. While learning Python, her teacher has assigned her an assignment to complete an incomplete program given below. The program intends to print from a csv file (people.csv) that stores tab separated fields as Empld Fname Lname. Phone 201 Vibhu Jain 295000 202 Nooh Kirk 610011 203, Haris Jamil 92055, 204 Kulwant Singh 69700 The incomplete code given to Diksha is as follows : import csv #Line1 with open(___, __) as myfile: #Line2 fin = csv (myfile, delimiter =~) “# Linea forrowin__: #Linea print(___) #Lines (a) Complete the Line2 of the given code so that given csv file is opened in read mode for processing. 1 (b) Complete Line3 so that the tab separated csv file is read into fin object. a (0) Complete the code of Lines 4 and 5 so that the records from the open csv file are printed line by line. : (4) Modify the code of Lines 4 and 5 so that only the fourth field (Phone) from each record gets printed. 1 (©) Write the output obtained by running the code of Lines 4 and 5. 1 378. Sonu Bhartiya has joined as an intern at Realsoft company. The company has department spread across India and the details ofall departments are stred in a file, Dept.eso, as showy below Dept_10 Dept Nome tecation | ow | Admin Delhi o20 | Production Hyderabad, oso | marketing Bangalore oso | agriResearch Mobsl oso | Agri:Macketing | Mohali D060 | Human Resources | Hyderabad por | Finance Delhi ‘pos | public Relations | Bangalore ‘Sonu has been asked to write a program to list the departments at a specific location. He has written the following code, with some wordsleyntax missing pee #Linet swith open( snewline=__) as csvfile: # Line? data = csv.reader(. ydeLiniter =" Linea ‘for row in data Lines 4f__inrow: Lines print( ) # Lines (a) Which library must be added to the program, for it to work ? 1 Fill Linel for this ©) Compete Line 2 30 that te fle storing the depron dts ges opened i suppressed newline processing. (©) The records are to be read row by row, not field by field. For this fil inthe Blank in Line 3. 1 (4) Sonu wants to print only the departments in location “Mohali”. Complete Lines 5 and 6 for the same. 1 (6) Convert the above given incomplete code so that location name is passed to a function Deplocation( ) and the function prints the rows of department dels from th locaton. 1 380. Ranjan Kumar of class 12 is writing a program to create a CSV file “user.csv” which wil) contain user name and password for some entries. He has written the following code. As 4 programmer, help him to successfully execute the given task. {CBSE Sample Paper 2020.91 import #Line 1 def addCsvF ile (UserName, Password # towrite/add data into the CSV file f= open(‘user.csv",*___ # Line 2 newFileWriter = csv.writer(F) newFileWriter.writerow([UserName, Passhord] ) F.close() csv file reading code def readCsvFile(): # to read data from CSV file with open(' user.csv’,"r*) as newFile: newFileReader=csv.___(newFile) # Line 3 for row in newFileReader: print (row[@],row[1]) newFile, Linea addCsvFile("Arjun”, "123@456") addCsvFile("Arunima”, “arugnima*) addCsvFile("Frieda”, “nyname@FRD") readCsvFile() #LineS (a) Name the module he should import in Line 1 1 (b) In which mode, Ranjan should open the file to add data into the file. i (0) Fill in the blank in Line 3 to read the data from a csv file (@) Fill in the blank in Line 4 to close the file. (©) Write the output he will obtain while executing Line 5. 1 387. Dhanush’s teacher has given him the following text fle (HigherEd.txt) Higher education improves an individual’ s quality of life. ‘Studies show that, conpared to high school graduates, college graduates have longer Life spans, better access tohealth care, better dietary and health Practices, greater economic stability and security. We must ensure that our whole population receives an education that will allow full and continuing participation in this dynamic period of economic history. and the following incomplete code def FileFunctiona( ‘#FiDL_Lines fin #FALL_Lines print(_ (wn) F411 Line? —__ ‘¥ri1L Lines print( (nay) ‘#F11_Lines main N #Fi11_Linea N=, #411 Line2 Filename = #Fi11_Line3 fileFunction1 (Filename, N1, N2) Lines Help Dhanush to complete his work as per the following instructions (@) Ade code to blank lines Fill_Line1 and Fill_Line2 so that two integer numbers are read into variables NI and N2. Also, complete Fill_Line3 so that the function call below it makes sense, which is sending 3 arguments; filename, and two numbers NI and N2. 1 (8) As per the function cal, complete the function header in Fill_LineS, 1 (©) Complete Fill_Line6 so that the text file is opened for reading in the file object fin. 1 (@ Complete Fill_Line7 and Fill_Line9 so that the passed number of characters are read from the fle and printed 1 (©) Complete Fil_Line so tat the characters til he end ofthe Tne from the current position ofthe fle pointer are read but not printed 1

You might also like