I am trying to go through a text file and take each line and put it in a dictionary. Example: If a txt file is used with
I am trying to create a dictionary like
word_dict = {'a': 1, 'b: 2', 'c': 3}
When I use this code:
def word_dict():
fin = open('words2.txt','r')
dict_words = dict()
i = 1
for line in fin:
txt = fin.readline().strip()
dict_words.update({txt: i})
i += 1
print(dict_words)
My dictionary contains only a partial list. If I use this code (not trying to build a dictionary just by testing):
def word_dict():
fin = open('words2.txt','r')
i = 1
while fin.readline():
txt = fin.readline().strip()
print(i,'.',txt)
i += 1
Same. It prints a list of incomplete values. However, the list matches the dictionary values. What am I missing?
source
share