CSV and JSON files · 9 / 15 Exercise Medium

Writing a CSV

Objective

Create a Python program that generates a CSV with csv.writer, letting the module take care of the quotes.

Instructions

  • Read an integer n and then, for each person, their name on one line and their age on the next.
  • Create a csv.writer over an io.StringIO(), with lineterminator="\n".
  • Write the header name,age first and then one row per person.
  • Show the generated content with print(output.getvalue(), end="").
  • If a name contains a comma, for example Pérez, Ana, the module will automatically write it in quotes.
Input
2
Ana
30
Luis
25
Output
name,age
Ana,30
Luis,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