Use app×
QUIZARD
QUIZARD
JEE MAIN 2026 Crash Course
NEET 2026 Crash Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
4.2k views
in Computer by (78.6k points)
closed by

A csv file "Happiness.csv" contains the data of a survey. Each record of the file contains the following data:

  • Name of a country 
  • Population of the country 
  • Sample Size (Number of persons who participated in the survey in that country) 
  • Happy (Number of persons who accepted that they were Happy)

For example, a sample record of the file may be: 

[‘Signiland’, 5673000, 5000, 3426]

Write the following Python functions to perform the specified operations on this file:

(I) Read all the data from the file in the form of a list and display all those records for which the population is more than 5000000.

(II) Count the number of records in the file.

1 Answer

+1 vote
by (77.1k points)
selected by
 
Best answer

(I)

def show():
    import csv
    f=open("happiness.csv",'r')
    records=csv.reader(f)
    next(records, None) #To skip the Header row
    for i in records:
       if int(i[1])>5000000:
          print(i)
    f.close()

(II)

def Count_records():
  import csv
  f=open("happiness.csv",'r')
  records=csv.reader(f)
  next(records, None) #To skip the Header row
  count=0
  for i in records:
         count+=1
  print(count)
  f.close()

Note (for both parts (I) and (II)):

(i) Ignore import csv as it may be considered the part of the complete program, and there is no need to import it in individual functions. 

(ii) Ignore next(records, None) as the file may or may not have the Header Row.

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...