Question: Please answer in Python (no list comprehension, no imports) Given a filepath to a .txt file containing the name, location, item and quantity information about

Please answer in Python (no list comprehension, no imports)

Given a filepath to a .txt file containing the name, location, item and quantity information about each persons trip and collected item on each line (always separated by commas). Write a function that returns a list of items in the same order as in the input file. Return an empty list if the file is empty. Assume there are no empty lines if the file is not empty and the file always exists. (Note: Item of quantity 0 still counts as collected item)

Hints: Note that when reading files, an entire line is represented as a long string. To get rid of extra spaces or newline characters at the end of a string, consider using str.strip() Now with a string of comma-separated elements, you could split it into a list and retrieve the information needed (do not forget about str.split()) List indices that are not -1, 0, 1 are also considered magic numbers.

The input file format for each line is: name,place,item,quantity

Example of an input file (for doctest 1): (See the first doctest for output) marina,hoover dam,rock,3 ella,las vegas,coins,100 ben,Lake Mead,fish,2

def items_only(filepath): """ >>> items_only('files/ings1.txt') ['rock', 'coins', 'fish'] >>> items_only('files/empty_trip.txt') [] """

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!