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

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: 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....
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...
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...
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns...
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns a new string that is a copy of the parameter where all the lowercase letters are kept as such, uppercase letters are converted to lowercase, and everything else is deleted. For example, the function call cleanLowerWord("Hello, User 15!") should return the string "hellouser". For this, you can start by copying the following functions discussed in class into your file: # Checks if ch is...
python 3 please Define a function voweliest that takes as input a string and returns as...
python 3 please Define a function voweliest that takes as input a string and returns as output a tuple where the string that has the most vowels in it is the first element and the second is the number of vowels in that string. Note: don't worry about ties or capital letters Hint: consider defining and using a separate function that counts the number of vowels in a given string
Python Implement function noVowel() that takes a string s as input and returns True if no...
Python Implement function noVowel() that takes a string s as input and returns True if no char- acter in s is a vowel, and False otherwise (i.e., some character in s is a vowel). >>> noVowel('crypt') True >>> noVowel('cwm') True >>> noVowel('car') False
Python. Create a function that receives the name of an auto maker and returns a slice...
Python. Create a function that receives the name of an auto maker and returns a slice of the dataframe with the cars from that auto maker. For instance, it may receive 'ford' and return a slice with all the cars produced by 'ford' meaning they have 'ford' in their name. Call the function for 'ford' to see if it works properly. Hint: You may create a list comprehension producing a list of all the index labels that include the auto-maker's...
build a python program that will be performing: - Read a CSV file 'annual.csv' enterprise into...
build a python program that will be performing: - Read a CSV file 'annual.csv' enterprise into a data structure - Count the number of rows and columns - Determine if the data contains empty values - Replace the empty values by 'NA' for strings, '0' for decimals and '0.0' for floats - Transform all Upper case characters to Lower case characters - Transform all Lower case characters to Upper case characters - save back the 'repaired' array as csv -...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT