Question

In: Computer Science

Language: Python Function name : sort_by_rating Parameters : file (.csv file), category (string), order (boolean) Returns:...

Language: Python

Function name : sort_by_rating
Parameters : file (.csv file), category (string), order (boolean)
Returns: None
Description : You want to see what order the items of a particular category in your file would be in if you sorted them by rating. Given a file, a category of clothing, and an order boolean (True for ascending order, False for descending order), create a function that writes a file called “sorted_items.csv” that includes all of the items in the specified category in sorted order by ranking.
The format of the file should look like this:

item name

rating

item name 1

rating 1

item name 2

rating 2

...

...

(NOTE: there will be a header row at the top of each .csv file)

Note: If your input file contains no items in the specified category, your “sorted_items.csv” file should still be created and have the header row. Also, your written files should end in a new line character
Hint : You can use the built-in Python .sort() method on a list of tuples. The tuples will get sorted by the first item in each tuple. Example:
>>> list_of_tups = [(5, "cats"), (6, "kittens"), (1, "lizards")] >>> list_of_tups.sort()
>>> print(list_of_tups)
[(1, 'lizards'), (5, 'cats'), (6, 'kittens')]

Test cases:

>>> sort_by_rating("uniqlo.csv","tops",True)

Solutions

Expert Solution

The python 3 code for your assignment is as: Use proper indentation as shown in screenshot.

*******************************writeCsv.py****************************************

import csv

def sort_by_ranking(fname,category,x):
data=[]
x=not x
with open(fname) as f:
reader = csv.DictReader(f, delimiter=',')
for row in reader:
  
name = row['item name']
rating = row['rating']
cat=row['category']
if category==cat:
data.append((name,rating))
  

data.sort(key=lambda x:x[1],reverse=x)
data.insert(0, ('item name','rating'))
  
with open('sorted_items.csv', 'w') as f:
writer = csv.writer(f , lineterminator='\n')
for item in data:
writer.writerow(item)
  
  
sort_by_ranking('uniqlo.csv','tops', True)


Related Solutions

Write a C++ function that reads a .csv file(file contains rows of string(name) and number) into...
Write a C++ function that reads a .csv file(file contains rows of string(name) and number) into a vector and loop through that vector and find the max number.
4. Implement the function read_info_file that consumes a file name (string) as its parameter and returns...
4. Implement the function read_info_file that consumes a file name (string) as its parameter and returns a list of strings - one element for each line in the file. These lines should have all the whitespace removed from both ends of the line. a. See the formatting of the individual_info data file. Consider how a file can be read into the program. In Python language
PYTHON LANGUAGE Write the data in variable artist_songs into a csv file, 'songs.txt', where the first...
PYTHON LANGUAGE Write the data in variable artist_songs into a csv file, 'songs.txt', where the first column is singer name and second column is a song name. Each line should have a singer and a song name. Use "Name" and "Song" as headers. Do not include double quotation marks (") in your CSV but you should include apostrophes where necessary (for example, for the song "We Don't Talk Anymore"). In [110]: artist_songs = { 'Taylor Swift': ['Love Story', 'You need...
PYTHON: Write a function named is_palindrome() that returns boolean True if a word or phrase is...
PYTHON: Write a function named is_palindrome() that returns boolean True if a word or phrase is a palindrome, and boolean False if it is not. The palindromes for this problem are contained in the list below. You must use this list in your solution. Use a for loop to retrieve each palindrome and test it. Your script should test each string in the list of palindromes below. Be aware there's a second list of palindromes embedded in the palindromes list....
In Python: Write a function called sum_odd that takes two parameters, then calculates and returns the...
In Python: Write a function called sum_odd that takes two parameters, then calculates and returns the sum of the odd numbers between the two given integers. The sum should include the two given integers if they are odd. You can assume the arguments will always be positive integers, and the first smaller than or equal to the second. To get full credit on this problem, you must define at least 1 function, use at least 1 loop, and use at...
Implement function get_contact(contacts, name) that returns a string. The contacts and the name parameter are both...
Implement function get_contact(contacts, name) that returns a string. The contacts and the name parameter are both type string. This function checks for the name string in the contacts string and returns that person’s contact information. If the person is not found, the function returns, “name not in contact”. Assume input is always valid. Assume the same name is not repeated in contacts. [You may use split(), range(), len() ONLY and no other built-in function or method] Examples:               contacts = “Frank...
Implement function get_contact(contacts, name) that returns a string. The contacts and the name parameter are both...
Implement function get_contact(contacts, name) that returns a string. The contacts and the name parameter are both type string. This function checks for the name string in the contacts string and returns that person’s contact information. If the person is not found, the function returns, “name not in contact”. Assume input is always valid. Assume the same name is not repeated in contacts. [You may use split(), range(), len() ONLY and no other built-in function or method] Examples:               contacts = “Frank...
Problem 9 - PYTHON There is a CSV-formatted file called olympics2.csv. Write code that creates a...
Problem 9 - PYTHON There is a CSV-formatted file called olympics2.csv. Write code that creates a dictionary named country_olympians where the keys are country names and the values are lists of unique olympians from that country (no olympian's name should appear more than once for a given country). Name,Sex,Age,Team,Event,Medal A Dijiang,M,24,China,Basketball,NA A Lamusi,M,23,China,Judo,NA Gunnar Nielsen Aaby,M,24,Denmark,Football,NA Edgar Lindenau Aabye,M,34,Sweden,Tug-Of-War,Gold Christine Jacoba Aaftink,F,21,Netherlands,Speed Skating,NA Christine Jacoba Aaftink,F,21,Netherlands,Speed Skating,NA Christine Jacoba Aaftink,F,25,Netherlands,Speed Skating,NA Christine Jacoba Aaftink,F,25,Netherlands,Speed Skating,NA Christine Jacoba Aaftink,F,27,Netherlands,Speed Skating,NA Christine...
4.31 Implement function duplicate() that takes as input the name (a string) of a file in...
4.31 Implement function duplicate() that takes as input the name (a string) of a file in the current directory and returns True if the file contains duplicate words and False otherwise. duplicate('Duplicates.txt') True duplicate('noDuplicates.txt') False Please solve using Python Language and without using str.maketrans please. Just simple programming, Thank youuuuu!!!!!
Use Python Write a function that takes a mobile phone number as a string and returns...
Use Python Write a function that takes a mobile phone number as a string and returns a Boolean value to indicate if it is a valid number or not according to the following rules of a provider: * all numbers must be 9 or 10 digits in length; * all numbers must contain at least 4 different digits; * the sum of all the digits must be equal to the last two digits of the number. For example '045502226' is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT