Question

O In market basket.txt file, there are lots of items in baskets. 1 line means 1 basket each. Every item is delimited with comma write items in a new file items.txt [result) O Find all items without duplication. Display the number of items, and [run] items.txt-메모장 파일(F) 편집(E) 서식(0) itrus fruit emi-finished bread he number of items IS 4 보기 도움말(H) rgar ine eady soups ropical fruit urt offee ole milk ip fruit ream cheese at spreads ther vegetables densed mi lk long life bakery product O Tip : Use str.replace0 function tter 1 се brasive cleaner

please us python3. Thanks.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def main():
    try:
        with open("market basket.txt", "r") as f, open("items.txt", "w") as w:
            items_list = []
            for basket in f:
                basket = basket.replace("\n", "").replace("\r", "")
                for item_in_basket in basket.split(","):
                    item_in_basket = item_in_basket.lower()
                    if item_in_basket not in items_list:
                        items_list.append(item_in_basket)
            for item_in_list in items_list:
                w.write(item_in_list + "\n")

            print("The number of items is " + str(len(items_list)) + ".")
    except FileNotFoundError:
        print("market basket.txt does not exist!")


main()
Add a comment
Know the answer?
Add Answer to:
please us python3. Thanks. O In 'market basket.txt' file, there are lots of items in baskets....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT