Skip to content
Question

Q.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".

CBSECBSE Class XII Board 2026Subjective· 4mImportance★★★★★
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

Read "States.csv", filter records where population exceeds 10,000,000, and write only those data rows (excluding the header) to "More.csv".

The task is a classic file input-output operation with filtering — a fundamental pattern in data processing. You have a source file containing structured information about Indian states, and you need to extract a subset of that data based on a criterion (population threshold) and write it to a new file. The challenge lies in correctly reading CSV data, applying the filter, and ensuring the header row is handled properly.

Python's csv module is purpose-built for this. When you open "States.csv", the first row is a header describing the columns. You need to skip copying this header to the output file, but you still need to read past it in the input file. Each subsequent row is a list of values: state name, capital, population (as a string that must be converted to an integer for comparison), and language.

The filtering condition is straightforward: if the population (third element, index 2) is greater than 10,000,000, write that entire record to "More.csv". Notice that the population in the file is stored as a string like '52221000', so you must convert it to an integer before comparing.

Here's how the program unfolds. Open the input file in read mode and the output file in write mode. Create a CSV reader for the input and a CSV writer for the output. Read the header from the input file first — this advances the file pointer past it, so subsequent reads give you only data rows. Then iterate through each remaining row, check the population, and if it exceeds the threshold, write that row to the output file.

import csv

# Open both files: input for reading, output for writing
with open('States.csv', 'r') as infile, open('More.csv', 'w', newline='') as outfile:
    reader = csv.reader(infile)
    writer = csv.writer(outfile)
    
    # Read and discard the header row
    header = next(reader)
    
    # Process each data row
    for row in reader:
        # row[2] is the population (as a string)
        population = int(row[2])
        
        # Filter: write only if population > 10,000,000
        if population > 10000000:
            writer.writerow(row)
Note

The newline='' parameter when opening the output file in write mode is important on Windows — it prevents extra blank lines between rows in the CSV. On Unix-like systems it's harmless, so it's good practice to include it always. …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.