Codington
0 of 6 problems solved • 0%

Exercise prompt

Export a Contact Sheet

A CSV writer quotes and escapes cells that contain punctuation. To build a string, write the rows into an empty io.StringIO() and return its getvalue().

Example

Write one row containing punctuation, then read it back to check where its fields end.

import csv from io import StringIO buffer = StringIO() csv.writer(buffer).writerow(["A, B", 'size "M"']) row = next(csv.reader(StringIO(buffer.getvalue()))) print(row) # ['A, B', 'size "M"']

The writer adds the required quoting. Build a complete sheet by deciding which header and data rows to send to it.

Your task

Implement contacts_csv(contacts). Write the header name,city, then one row per contact dictionary in the supplied order. Handle commas and quotation marks in either field. An empty input still needs a header.

contacts_csv([{"name": "Jo", "city": "Lund"}]) # header followed by Jo,Lund