CSV and JSON files · 15 / 15 Exercise Medium

From CSV to JSON

Objective

Create a Python program that converts data in CSV format to JSON.

Instructions

  • Read an integer n, then a header line name,age and then n data rows.
  • Read the rows with csv.DictReader, passing it the list made of the header and the rows.
  • Convert the age of each row into an integer.
  • Show the list of dictionaries converted to JSON with json.dumps(list, ensure_ascii=False).
Input
2
name,age
Ana,30
Luis,25
Output
[{"name": "Ana", "age": 30}, {"name": "Luis", "age": 25}]

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

Start exercise