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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started