0

So I have a txt file with word(s) and their respective score as an integer in each line. Example:

word_scores.txt:

abandon -2

accept +2

cheer 4

give up -6

I tried this code:

d = {} #store in dictionary d
with open("word_scores.txt") as f:
    for line in f:
       (key, val) = line.split() #key = word, val = score
       d[key] = int(val)

It works fine except when there are two words in one line (eg, give up). In this case, how can I modify my code so it can successfully store "give up" as a key in the dictionary?

Thank you in advance

parkjw96
  • 27
  • 3

1 Answers1

2
>>> "give up -6".rsplit(" ", 1)
['give up', '-6']
Rory Browne
  • 627
  • 1
  • 5
  • 11