#C13556. CSV to Dictionary Converter
CSV to Dictionary Converter
CSV to Dictionary Converter
You are given CSV data from the standard input. The first line contains the headers (column names) separated by commas, and each subsequent line contains row data. Your task is to convert this CSV data into a dictionary where each key is a column header and the corresponding value is a list of entries under that column.
If a data row is missing one or more fields (i.e. inconsistent row lengths), treat the missing fields as None (i.e. a null value). In case of any error during processing, output an empty dictionary {}
.
Your output must be in the Python dictionary format. For example, given the input:
name,age,city Alice,30,New York Bob,25,Boston Charlie,35,Chicago
your program should print:
{'name': ['Alice', 'Bob', 'Charlie'], 'age': ['30', '25', '35'], 'city': ['New York', 'Boston', 'Chicago']}
Note: When printing a missing field, print it as None
(without quotes).
inputFormat
The input is read from standard input (stdin) and consists of multiple lines. The first line is a comma‐separated list of column headers and each subsequent line represents a row of CSV data.
outputFormat
Output to standard output (stdout) the resulting dictionary in the Python dictionary format. Use single quotes for strings and print None
(without quotes) for any missing value.
name,age,city
Alice,30,New York
Bob,25,Boston
Charlie,35,Chicago
{'name': ['Alice', 'Bob', 'Charlie'], 'age': ['30', '25', '35'], 'city': ['New York', 'Boston', 'Chicago']}