Question:

A csv file "States.csv" contains some data about all the states of India. Each record of the file contains the following data: Name of the State, Capital of the State, Population of the State, Official Language of the State.

For example, a sample record in the file is:

['Andhra Pradesh','Amaravati',52221000,'Telugu']

Write a Python program which reads the data from this file and appends all those records where population is more than 10000000 into another csv file 'More.csv'.

Note: "States.csv" also contains the Header row. The Header row should NOT be copied to "More.csv".

Show Hint

Use csv.reader on States.csv, skip the header with next(), and write rows with int(row[2]) above 10000000 using csv.writer in append mode.
Updated On: Oct 20, 2026
Show Solution
collegedunia
Verified By Collegedunia

Solution and Explanation

Step 1: Plan the program.
We must read every record of States.csv, skip the header row, and copy the records with a population above 10000000 to More.csv.
The csv module gives the tools: csv.reader to read and csv.writer to write.

Step 2: Open both files.
Open States.csv in read mode ('r').
Open More.csv in append mode ('a'), because the question says the records are appended. Use newline='' so no blank line appears between rows.

Step 3: Skip the header.
Create the reader object and call next() once. This reads the first row (the header) and moves past it, so the header is never copied.

Step 4: Test the population.
Each record is a list of strings. The population is at index 2, so it must be changed with int() before comparing.
If int(rec[2]) > 10000000, write the record with writerow().

Step 5: Write the full program.
import csv
f1 = open('States.csv', 'r')
f2 = open('More.csv', 'a', newline='')
r = csv.reader(f1)
w = csv.writer(f2)
next(r)
for rec in r:
    if int(rec[2]) > 10000000:
        w.writerow(rec)
f1.close()
f2.close()

Step 6: Test result.
The program was run on a sample file of 4 states. Only the states with a population above 10000000 were copied to More.csv, and the header row was not copied.

Final Answer:
The program above copies all records with population more than 10000000 into More.csv and leaves out the header row.
Was this answer helpful?
0
0