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

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....
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
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 -...
On Python Write a function that accepts a relative file path as parameter and returns number...
On Python Write a function that accepts a relative file path as parameter and returns number of non-empty lines in the file. Test your function with the provided sample file studentdata.txt.
Use python. redact_file: This function takes a string filename. It writes a new file that has...
Use python. redact_file: This function takes a string filename. It writes a new file that has the same contents as the argument, except that all of the phone numbers are redacted. Assume that the filename has only one period in it. The new filename is the same as the original with '_redacted' added before the period. For instance, if the input filename were 'myfile.txt', the output filename would be 'myfile_redacted.txt'. Make sure you close your output file.
Python Coding Create function openAndReturnLastN() to meet the conditions below - accept 2 parameters; 1 string...
Python Coding Create function openAndReturnLastN() to meet the conditions below - accept 2 parameters; 1 string (name of a file) and 1 integer (N) - add a docstring - use exception handling techniques to attempt to open the filename provided, if it does not exist, return False - the file will contain an unknown number of lines - store the last N lines of the file in a single string var - return the resultant string var --- ensure to...
Python Programcomplete theprocessString(string)function. This function takesin a string as a parameter and prints the...
Python Program complete theprocessString(string)function. This function takes in a string as a parameter and prints the average number of characters per word in each sentence in the string. Print the average character count per word for each sentence with 1 decimal precision(see test cases below).-Assume a sentence always ends with a period (.)or when the string ends. -Assume there is always a blank space character(" ")between each word. -Do not count the blank spaces between words or the periods as...
PYTHON. Tasks: Create and test the openFileReadRobust() function to open file (for reading) whose name is...
PYTHON. Tasks: Create and test the openFileReadRobust() function to open file (for reading) whose name is provided from the standard input (keyboard) – the name is requested until it is correct Create and test the openFileWriteRobust () function to open file (for writing) whose name is provided from the standard input (keyboard) – the name is requested until is correct Extend the program to count number of characters, words and lines in a file as discussed in the classby including...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT