<br><br><div><span class="gmail_quote">On 3/6/06, <b class="gmail_sendername">Jerome Jabson</b> &lt;<a href="mailto:jjabson@yahoo.com">jjabson@yahoo.com</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;">
I'm having problems trying to parse a text file and<br>separating it's contents into a dictionary. The file<br>as something like:<br><br>name<br>This is the description of the name.<br><br>another_name<br>Another description of this name.
<br><br>The dictionary should look like:<br><br>dict = {name: This is the description of the name.,<br>another_name: Another discription of this name}<br><br>I tried doing something like:<br><br>dict = {}</blockquote><div>
<br>
don't ever use builtin names for variable names. It'll bite you. <br>
mydict = {}<br>
&nbsp;</div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">f = open(&quot;text-file&quot;, &quot;r)<br>for line in f:<br>&nbsp;&nbsp; items = line<br>
&nbsp;&nbsp;&nbsp;&nbsp;dict[items[0]] = items[1:]<br>print dict</blockquote><div><br>
<br>
</div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">but the dictionary comes out all wrong! :-( </blockquote><div><br>
<br>
Describe &quot;all wrong&quot;? From your code above, I suspect you get the first
letter of the name as the key, rather than the whole name. You need to
split the line into separate words then rejoin them with ' '.join() for
the value. <br>
</div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Can<br>someone give me some pointers on how to do this.</blockquote><div><br>
<br>
f = open(&quot;namer.txt&quot;, &quot;r&quot;)<br>
<br>
mydict = {}<br>
<br>
for line in f:<br>
&nbsp;&nbsp; l = line.split()<br>
&nbsp;&nbsp; mydict[l[0]] = ' '.join(l[1:])<br>
&nbsp;&nbsp; <br>
print mydict<br>
&nbsp;<br>
&nbsp;</div>HTH<br>
Anna</div><br>