<br><br><div><span class="gmail_quote">On 1/27/06, <b class="gmail_sendername">Bob Gailer</b> <<a href="mailto:bgailer@alum.rpi.edu">bgailer@alum.rpi.edu</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Bob Gailer wrote:<br>> Alan Gauld wrote:<br>><br>>> Hi Ben,<br>>><br>>><br>>><br>>>> I want to enter the words and definitions from the text file into the<br>>>> dict.<br>>>> The way the text file is set up is that one line is the word and the
<br>>>> next line is the definition.<br>>>><br>>>><br>>><br>>><br>>>> I tried using a for loop like this<br>>>><br>>>> f = open('glossary.txt','r')<br>>>> gloss = {}
<br>>>><br>>>> for line in f:<br>>>> gloss[line] = line<br>>>><br>>>><br>>> The problem that you have is that you really need to read two lines at a<br>>> time.
<br>>> (Assuming that the definitions are all on one line which may not be true!)<br>>> A while loop may be easier in this case.<br>>><br>>> A for loop will read each line individually. You then need to set a
<br>>> definition<br>>> flag to tell the loop body whether you are reading a definition or a key.<br>>><br>>> Either type of loop is possible. Since you started with a for loop lets<br>>> stick with it...
<br>>><br>>> definition = False<br>>> currentKey = None<br>>><br>>> for line in f:<br>>> if isDefinition:<br>>> gloss[currentKey] = line<br>>> currentKey = None
<br>>> isDefinition = False<br>>> else:<br>>> currentKey = line<br>>> isDefinition = True<br>>><br>>><br>> Or you can use next():<br>><br>> for line in f:
<br>> gloss[line] = f.next()<br>><br>Or even:<br>[gloss.setdefault(l,f.next()) for l in f]</blockquote></div><br>
Hello Bob<br>
<br>
I understand f.next(), but [gloss.setdefault(l,f.next()) for l in f] is beyond me at this point.<br>
Thanks for your input.<br>
<br>
Ben<br>