CSV and JSON files · 8 / 15 Exercise Medium

Filter rows by city

Objective

Create a Python program that reads a CSV with a header and shows the people of a city using csv.DictReader.

Instructions

  • Read an integer n.
  • Read a header line name,age,city and then n data rows.
  • Go through the rows with csv.DictReader, passing it the list made of the header and the rows. Each row will be a dictionary with the keys name, age and city.
  • Read the city to look for.
  • Show the name of each person who lives in that city, one per line and in the order of the file. If there are none, show None.
Input
4
name,age,city
ana,30,madrid
luis,25,lyon
eva,41,madrid
pepe,19,roma
madrid
Output
ana
eva

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise