Advertisements
Advertisements
Question
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:
- 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.
- Count the number of records in the file.
Code Writing
Solution
-
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()
-
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()
shaalaa.com
Notes
(for both parts (I) and (II)):
- 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.
- Ignore next(records, None) as the file may or may not have the Header Row.
Is there an error in this question or solution?