Question

def load_locations(filename): (str) -> dict Read data from the file with the given fi lename and create and return a dictiona[BEGIN DESCRIPTION There is nothing interesting here [END DESCRIPTION] BEGIN DESCRIPTION] You wake up lost in some unfamiliarload_locations should return a dictionary that is structured as follows: - Each key is a float that is the location ID - Each

Python Code help needed. Based on the file (2nd picture) a dictionary like the third picture is to be made.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

SOLUTION

f = open("file.txt", "r")
d={}
temp=0
for x in f:

    #if the line is [END DESCRIPTION] or [END ACTION] then don't store it.
    if len(x)==18 or len(x)==13:       
        temp=0
    #if the value of temp is 1 , we've encountered [END DESCRIPTION] start storing
    if temp==1:
        #store the next lines and remove the railing new lines
        d[val].append([x.strip()])        
    #if the value if temp is 2 , we've encountered [END ACTION] start storing again
    elif temp==2:
        #stripping the trailing white spaces and new line characters
        x=x.strip()
        #find the location of ' , 2.1 ' and store it in t
        t=x.find(", "+str(int(val))+".")  
        #if the dictionary has only 1 list by the key add one more for [START ACTION] lines
        if len(d[val])==1:
            #Adding another list
            d[val].append([])              

        #Adding lines after [START ACTION] in a tuple and using the position of ' , 2.1' in t
        d[val][1].append((x[:t],float(x[t+2:])))
    try:
        #Check if the line have number then convert it to float
        val=float(x)
        #Form the number as a key to an empty list
        d[val]=[]
    except ValueError:
    #if the line above doesn't have a number it'll encounter this part
    
        #assign 1 if line is [START DESCRIPTION] whose length is 20, 2 if line is [START ACTION] whose length is 15and 0 otherwise
        if len(x)==20:
            temp=1
        elif len(x)==15:
            temp=2
print(d)

HOPE THIS HELPS IF YOU LIKE THE SOLUTION PLEASE RATE IT.

THANKS!

Add a comment
Know the answer?
Add Answer to:
Python Code help needed. Based on the file (2nd picture) a dictionary like the third picture...
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
  • Please read the article and answer about questions. You and the Law Business and law are...

    Please read the article and answer about questions. You and the Law Business and law are inseparable. For B-Money, the two predictably merged when he was negotiat- ing a deal for his tracks. At other times, the merger is unpredictable, like when your business faces an unexpected auto accident, product recall, or government regulation change. In either type of situation, when business owners know the law, they can better protect themselves and sometimes even avoid the problems completely. This chapter...

  • I need help with my very last assignment of this term PLEASE!!, and here are the instructions: After reading Chapter T...

    I need help with my very last assignment of this term PLEASE!!, and here are the instructions: After reading Chapter Two, “Keys to Successful IT Governance,” from Roger Kroft and Guy Scalzi’s book entitled, IT Governance in Hospitals and Health Systems, please refer to the following assignment instructions below. This chapter consists of interviews with executives identifying mistakes that are made when governing healthcare information technology (IT). The chapter is broken down into subheadings listing areas of importance to understand...

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