I am hoping someone can help me here. I am having some serious trouble adding a single list, from a text file into a dictionary. the list in the text file appears as:
20
Gunsmoke
30
The Simpsons
10
Will & Grace
14
Dallas
20
Law & Order
12
Murder, She Wrote
What I need is for each entry, one line at a time, to become the key and then value. For example it should look like {20:Gunsmoke, etc...}
I have to use the file.readlines() method according to my instructor. currently my code looks like this:
# Get the user input
inp = input()
# creating file object.
open = open(inp)
# read the file into seperate lines.
mylist = open.readlines()
# put the contents into a dictionary.
mydict = dict.fromkeys(mylist)
print(mydict)
The out put looks like this:
file1.txt {'20\n': None, 'Gunsmoke\n': None, '30\n': None, 'The Simpsons\n': None, '10\n': None, 'Will & Grace\n': None, '14\n': None, 'Dallas\n': None, 'Law & Order\n': None, '12\n': None, 'Murder, She Wrote\n': None}
Process finished with exit code 0
There is more to this problem, but I am not here for someone to do my homework, I just cant figure out how to add this in properly. I have to be missing something and I am betting its simple. Thank you for your time.