Q36
4 marksShort AnswerSection D

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

File Handling in Python
CSV File Handling — Reading, Filtering and Writing Records
Official Answer

A complete working program that reads States.csv, skips its header, and copies only high-population rows into More.csv.


``python

import csv


def copypopulousstates():

with open('States.csv', 'r', newline='') as f_in:

reader = csv.reader(f_in)

next(reader) # skip header row

with open('More.csv', 'w', newline='') as f_out:

writer = csv.writer(f_out)

for row in reader:

if int(row[2]) > 10000000: # row[2] is Population

writer.writerow(row)


copypopulousstates()

``

csv modulecsv.reader()csv.writer()next() to skip headerwriterow()file handling in pythonint() type conversion

Marking Scheme

  • 11 mark for correctly opening States.csv for reading using the csv module (csv.reader).
  • 21 mark for skipping the header row using next(reader).
  • 31 mark for correctly checking int(row[2]) > 10000000 to filter by population.
  • 41 mark for opening More.csv for writing and correctly writing each qualifying row with csv.writer().writerow().

Hint

Use csv.reader() and csv.writer(); call next(reader) once to skip the header; cast Population to int() before comparing.

Quick Oral Answer

I read States.csv with csv.reader, skip the header using next(), then for every row whose Population (converted to int) exceeds one crore, I write that row to More.csv using csv.writer's writerow().

Analysis & Explanation

The task combines three standard file-handling skills — reading a CSV, skipping its header, and conditionally copying rows to another CSV.


Concept

  • csv.reader(f_in) turns each line of States.csv into a list like ['Andhra Pradesh','Amaravati',52221000,'Telugu'].
  • next(reader) advances the iterator past the header row exactly once, so it is never written to More.csv.
  • Population sits at index 2 (0: Name, 1: Capital, 2: Population, 3: Language); it must be cast to int() since values read from a text-mode CSV file arrive as strings.

Why the condition matters

  • Only rows with Population > 10000000 (one crore) qualify; every other row is skipped entirely.

Exam trap

  • Comparing row[2] (a string) with 10000000 directly without int() conversion, which compares strings lexicographically and gives wrong results.
  • Forgetting next(reader), which would incorrectly test/write the header row itself.

Common Mistakes

  1. 1Comparing the Population field as a string (e.g. row[2] > '10000000') instead of converting it with int() first, which produces incorrect string comparisons.
  2. 2Forgetting to skip the header row with next(reader), causing the header itself to be tested and possibly written into More.csv.
  3. 3Opening the CSV files without newline='', which can introduce blank lines between rows on Windows.

Interesting Facts

Python's csv module automatically handles commas embedded inside quoted fields, something a naive line.split(',') approach would break on.

next(reader) works because a csv.reader object is an iterator; calling next() on any iterator advances it by exactly one item.

Passing newline='' when opening a CSV file in text mode is officially recommended by the Python documentation to prevent extra blank rows caused by universal newline translation.

Spotted a mistake or something unclear?

Tell us — we fix reported answers fast.

Frequently Asked Questions

Why must row[2] be converted with int() before comparing?

Because csv.reader() always returns each field as a string; comparing a string like '52221000' with the integer 10000000 without conversion would not perform the intended numeric comparison.

How is the header row excluded from More.csv?

By calling next(reader) once immediately after opening the reader, which advances past the first (header) row before the for-loop begins reading data rows.

Could the DictReader/DictWriter classes be used instead?

Yes — csv.DictReader would let you access fields by name (row['Population']) instead of index, achieving the same filtering with more readable code.