Question: How do I do these three questions? 3. add_rows [15] Write a function called add_rows which takes a CSV filename string and a list of
How do I do these three questions?
![How do I do these three questions? 3. add_rows [15] Write a](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/10/670ff1e078fc2_208670ff1e0586d0.jpg)


3. add_rows [15] Write a function called add_rows which takes a CSV filename string and a list of dictionaries (which represent rows) as arguments and appends each of the rows in order to the end of the file. If an empty list is given, nothing should change. Hint: Since you are adding rows represented as dictionaries, you will find the csv. DictWriter object useful. In order for it to know which columns to put the values for each row dictionary, you will need to add an additional fieldnames argument to the csv. DictWriter constructor and assign it the list of column name strings. You can do this by getting a list of keys in the first row (if the list is non-empty) and assigning it like so: # . . . keys = # code to get keys of first row dictionary writer = csv. DictWriter(csvfile, fieldnames=keys) # rest of your code to append each row dictionary .. . > >> rows = load_data('pts_mini . csv' ) > > > rows [{ 'x ': '0', 'y': '1'}, { 'x ': '2", 'y": "-1' }] > > > rows >>> add_rows ( 'pts_mini . cav', [{ 'x' : 2, 'y' : 3 }, {'x': 1, 'y': 0}]) > > > rows = load_data( 'pts_mini . csv' ) > > > rows [{ 'x ': '0', 'y': '1'}, { 'x ': '2', 'y ': '-1'}, { 'x ' : '2 ' , 'y': '3'} , { 'x ': '1 ', 'y ' : '0'} ]4. filter_rows [15] Write a function filter_rows which takes a list of dictionaries, a column name string, and a value and returns a filtered list of only the row dictionaries that have the given value for the given column name key (using == ). > > > rows = [{ 'x ': ' 0 ' , ' y ' : ' 1 ' } , { 'x ' : ' 2 ' , ' y ' : ' - 1 ' } , { 'x ': ' 0 ' , ' y ' : '2' } ] > > > rows2 = filter_rows (rows, 'x' , '0') > > > rows2 [{ 'x ': '0', 'y': '1'}, {'x': '', 'y': '2'}] > > > rows2 = filter_rows(rows, 'x' , '-1') > > > rows2 > > > rows2 = filter_rows (rows, 'z' , '0') > >> pokedex = load_data( 'pokedex . csv' ) > >> the_very_best = filter_rows(pokedex, 'name' , 'Pikachu' ) > > > the_very_best [{'id' : '25', 'name' : 'Pikachu', 'description': 'El's favorite Pokemon! When several Pikachu gather, 'stage' : '1', 'type': 'Electric', 'weakness' : 'Ground', 'hp': '160', 'movel': 'Growl' , 'move2' : 'Thunderbolt', 'move3': 'Quick Attack', 'move4': "'}]5. filter_column [20] Write a function filter_column which takes a CSV filename string and a column name string and returns a list of all data for the given column (ignoring letter-casing) in order of the rows in that file. You can assume that the given file is a .csv file that exists and that there is no more than one column matching the column string argument. If the column is not found in the CSV file return Note that while filter_rows takes a list of dictionaries, filter_column takes a CSV filename string. Use csv. reader or csv. DictReader . In addition to your code, provide 1-2 sentences in a comment above the function header justifying your choice for one over the other in this problem. > >> filter_column( 'pts_mini. csv' , 'x' ) ['0' , '2'] > > > filter_column( 'pts_mini. csv' , 'y' ) [ '1 ' , ' - 1' ] >>> data = filter_column( 'pokedex . csv' , 'name' ) > > > len (data) 151 > > > data [ : 5] [ ' Bulbasaur' , 'Ivysaur', 'Venusaur', 'Charmander', 'Charmeleon' ] > > > data [ - 5 : ] [ 'Dratini' , 'Dragonair', 'Dragonite', 'Mewtwo' , 'Mew' ]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
