<br><br><div><span class="gmail_quote">On 1/27/06, <b class="gmail_sendername">Bob Gailer</b> &lt;<a href="mailto:bgailer@alum.rpi.edu">bgailer@alum.rpi.edu</a>&gt; 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>&gt; Alan Gauld wrote:<br>&gt;<br>&gt;&gt; Hi Ben,<br>&gt;&gt;<br>&gt;&gt;<br>&gt;&gt;<br>&gt;&gt;&gt; I want to enter the words and definitions from the&nbsp;&nbsp;text file into the<br>&gt;&gt;&gt; dict.<br>&gt;&gt;&gt; The way the text file is set up is that one&nbsp;&nbsp;line is the word and the
<br>&gt;&gt;&gt; next line is the definition.<br>&gt;&gt;&gt;<br>&gt;&gt;&gt;<br>&gt;&gt;<br>&gt;&gt;<br>&gt;&gt;&gt;&nbsp;&nbsp;I tried using a for loop like this<br>&gt;&gt;&gt;<br>&gt;&gt;&gt;&nbsp;&nbsp;f = open('glossary.txt','r')<br>&gt;&gt;&gt;&nbsp;&nbsp;gloss = {}
<br>&gt;&gt;&gt;<br>&gt;&gt;&gt;&nbsp;&nbsp;for line in f:<br>&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gloss[line] = line<br>&gt;&gt;&gt;<br>&gt;&gt;&gt;<br>&gt;&gt; The problem that you have is that you really need to read two lines at a<br>&gt;&gt; time.
<br>&gt;&gt; (Assuming that the definitions are all on one line which may not be true!)<br>&gt;&gt; A while loop may be easier in this case.<br>&gt;&gt;<br>&gt;&gt; A for loop will read each line individually. You then need to set a
<br>&gt;&gt; definition<br>&gt;&gt; flag to tell the loop body whether you are reading a definition or a key.<br>&gt;&gt;<br>&gt;&gt; Either type of loop is possible. Since you started with a for loop lets<br>&gt;&gt; stick with it...
<br>&gt;&gt;<br>&gt;&gt; definition = False<br>&gt;&gt; currentKey = None<br>&gt;&gt;<br>&gt;&gt; for line in f:<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; if isDefinition:<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gloss[currentKey] = line<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;currentKey = None
<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;isDefinition = False<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; else:<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;currentKey = line<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;isDefinition = True<br>&gt;&gt;<br>&gt;&gt;<br>&gt; Or you can use next():<br>&gt;<br>&gt; for line in f:
<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; gloss[line] = f.next()<br>&gt;<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>