Answered step by step
Verified Expert Solution
Question
1 Approved Answer
a python csv question. the 1st question probably require nested loop import csv, json alist ((Legends, Nickname), (Kobe Bryant, Mamba), (Shaq, Shaq Daddy), [Calvin Johnson,
a python csv question. the 1st question probably require nested loop
import csv, json alist (("Legends", "Nickname"), ("Kobe Bryant", "Mamba"), ("Shaq", "Shaq Daddy"), ["Calvin Johnson", "Megatron"]] Question 1 - Return a list of dictionaries with the keys being the fieldnames and the values being the respective values in each row - The parameter alist is a list of lists, with the oth index being the fieldnames - DO NOT HARD CODE the keys to each dictionary, we will test your code with different data . - e.g >> alist = (("Legends", "Nickname"), ("Kobe Bryant", "Mamba"), ("Shaq", "Shaq Daddy"), ("Calvin Johnson", "Megatron"]] >> print(list_of_lists_to_list_of_dicts(atist)) ('Legends': 'Kobe Bryant', 'Nickname': 'Mamba'), ('Legends': 'Shaq', 'Nickname': 'Shaq Daddy'), ('Legends': 'Calvin Johnson', 'Nickname''Megatron'] def list_of_lists_to_list_of_dicts(alist): pass Question 2 - Write a function that writes the header and each row of data to the csv file name specified in the parameter. Return the number of rows written to the file, NOT including the header ters alist follows the same structure as alist in Questioni, Tilename will include the file extension (e.g. "filename.csv") - Hint: call list_of_lists_to_list_of_dicts to convert the list of lists to list of dicts before writing using csv.Dictwriter e.g. >> alist = [["Legends","Nickname"), ("Kobe Bryant", "Mamba"), ("Shaq", "Shaq Daddy"] , ["Calvin Johnson", "Megatron"]] >> print(list_of_lists_to_cavalist, 'output.csv')); The example csv should look like the following: Legends,Nickname Kobe Bryant, Mamba Shaq, Shaq Daddy Calvin Johnson, Megatron def list_of_lists_to_csv(alist, filename): pass Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started