[Tutor] Trying to enter text from a file to a Dictionary

Ben Markwell ben3941 at yahoo.com
Fri Jan 27 05:49:35 CET 2006


Being new to programming, I made a text file that  contains terms with their definitions that I have come across in my  studying. As an exercise, I thought  I would make a glossary using  a dictionary so I can look up words, add new words, view all entries,  etc.  
 I want to enter the words and definitions from the  text file into the dict. The way the text file is set up is that one  line is the word and the next line is the definition. 
  
  For example:
  Word
      Definition of the word.
  Second Word
      Definition of the second word.
  Etc....
  
  I thought this would be rather simple (maybe it really is), but I'm  stuck.  I can't figure out how to tell Python to read the line  with the word, add the word to the Key of the dict, then read the next  line and add that line to the Value of the dict, then do it all again  'til the end of the file.
  
  I tried using a for loop like this
  
  f = open('glossary.txt','r')
  gloss = {}
  
  for line in f:
      gloss[line] = line
  
  When I tried to print this, a huge list of key/values printed out. With  many copies of the same key/value pair.   And the key and the  value were the same. (not word followed by definition) , which  afterword made sense, because, I told it that 
  gloss[line] = line
  
  I tried :
  
  for line in f:
      gloss[line] = f.readline()
  
  That got me nowhere. Sorry for being so dense. I understand what needs to happen, but am having trouble implementing it. 
  
  Thanks for your help and understanding.
  

		
---------------------------------
Bring words and photos together (easily) with
 PhotoMail  - it's free and works with Yahoo! Mail.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060126/f52cfe5b/attachment.html 


More information about the Tutor mailing list