Question

Solve in PYTHON Code ONLY please!

In-Class: Radishes Using the text file, radishsurvey.txt Write a script that will read the file, (strip the carriage returns)

radishsurvey.txt Mary johnson,Black Radish Jimmy Buffet, Daikon Radish Tommy Morris,French Breakfast Radish George Jones,hors

In-Class: Radishes Using the text file, radishsurvey.txt Write a script that will read the file, (strip the carriage returns), create a dictionary of names and favorite radishes and create a dictionary of each radish type and the number of people who chose that as their favorite. Examples: First dictionary: l"AngelinaBelmore": "Plum Purple", "Fred Smith": "Red king"] Second dictionary: {"Plum Purple": 2, "Red king": 7) Note that in the text file, some have used capital letters and lower case letters for the same variety - clean this up!
radishsurvey.txt Mary johnson,Black Radish Jimmy Buffet, Daikon Radish Tommy Morris,French Breakfast Radish George Jones,horseradish john Camp,round radish Melissa Dark,watermelon radish Alice Chang,Early Scarlet Globe Radish Stefanie Smith,German Giant Radish Jeff Black,China Rose Radish Jimmy Newman,horseradish Tammy George,horseradish Jianqing Liu,horseradish
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is your code in Python :D

=======================================================================================

def organize():
    fx = open("radishsurvey.txt", "r")      # Open File
    data = fx.read().split("\n")    # Make a list of entries by reading and then splitting by newline
    people_dict = {}                        # Initialization of 2 dictionaries
    radish_dict = {}

    for info in data:                       # For each entry
        item = info.split(",")              # Split Person's Name and favourite Radish

        # So, item[0] is the person's Name and item[1] is his/her favourite radish
        item[0] = cleanword(item[0])        # Calling Clean Word Function to capitalize the words
        item[1] = cleanword(item[1])

        people_dict[item[0]] = item[1]      # Adding dictionary entries to the first dictionary

        if item[1] in radish_dict:          # For second dictionary, if radish already present, increase its frequency
            radish_dict[item[1]] += 1

        else:                               # Else, make new entry and initialize frequency to 1
            radish_dict[item[1]] = 1

    print(people_dict)                      # Print both of the dictionaries
    print(radish_dict)


def cleanword(string):
    words = string.split(" ")               # Separate the string by spaces
    newword = ""                            # Result clean string to be stored in a new word
    for word in words:
        newword += word.capitalize() + " "  # Capitalize the word, and add space after it for next word(like surname)
    return newword.strip(" ")               # Remove the last space


organize()

======================================================================================

Here are the screenshots to make sure of the indentations

OUTPUT FOR THE GIVEN ABOVE RADISH TXT FILE

{'Mary Johnson': 'Black Raddish', 'Jimmy Buffet': 'Daikon Radish', 'Tommy Morris': 'French Breakfast Radish', 'George Jones': 'Horseradish', 'John Camp': 'Round Radish', 'Melissa Dark': 'Watermelon Radish', 'Kim Jordan': 'China Rose Radish', 'Alice Change': 'Early Scarlet Globe Radish', 'Stefanie Smith': 'German Giant Radish', 'Jeff Black': 'China Rose Radish', 'Jimmy Newman': 'Horseradish', 'Tammy George': 'Horseradish', 'Jianqing Liu': 'Horseradish'}


{'Black Raddish': 1, 'Daikon Radish': 1, 'French Breakfast Radish': 1, 'Horseradish': 4, 'Round Radish': 1, 'Watermelon Radish': 1, 'China Rose Radish': 2, 'Early Scarlet Globe Radish': 1, 'German Giant Radish': 1}

If you have any queries, do drop a comment and I will surely respond :D. And if you liked the answer, do give an upvote :)

Add a comment
Know the answer?
Add Answer to:
In-Class: Radishes Using the text file, radishsurvey.txt Write a script that will read the file, ...
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
  • C++ Create an application that searches a file of male and female first names. A link...

    C++ Create an application that searches a file of male and female first names. A link to the file is provided on the class webpage. "FirstNames2015.txt" is a list of the most popular baby names in the United States and was provided by the Social Security Administration. Each line in the file contains a boy's name and a girl's name. The file is space-delimited, meaning that the space character is used to separate the boy name from the girl name....

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
ADVERTISEMENT