<div>Tyler,</div><div><br></div><div>This is a simple error the KeyError is caused because the key isn&#39;t in the dictionary <a href="http://docs.python.org/library/exceptions.html#exceptions.KeyError" target="_blank">http://docs.python.org/library/exceptions.html#exceptions.KeyError</a> and easy fix you can either check for the key prior to using or use the get method</div>

<div>
<br></div><div>1) rgenre = re.split(r&#39;;&#39;, <a href="http://rf.info">rf.info</a>[&quot;genre&quot;]) if &quot;genre&quot; in <a href="http://rf.info">rf.info</a> else []</div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div>

the regex will never get executed here and an empty list will be returned if genre not present</div><div><br></div><div>2) rgenre = re.split(r&#39;;&#39;, rf.info.get(&quot;genre&quot;, &#39;&#39;))</div><div>the get will not throw this error and return &#39;&#39; if &#39;&#39;genre&#39; is not found in the dictionary</div>


<div><br></div><div>Hope that helps,</div><div><br></div><div>Vince</div><div><div><a href="http://docs.python.org/library/exceptions.html#exceptions.KeyError" target="_blank"></a><br><br><div class="gmail_quote">On Mon, Sep 13, 2010 at 10:46 PM,  <span dir="ltr">&lt;<a href="mailto:aeneas24@priest.com" target="_blank">aeneas24@priest.com</a>&gt;</span> wrote:<br>


<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><font color="black" size="2" face="arial">
<div><font face="Arial, Helvetica, sans-serif">Hi Vince,</font></div>


<div> </div>


<div>Thanks very much for the one-line version--unfortunately, I still get errors. The overall script runs over every text file in a directory, but as soon as it hits a text file without a &lt;genre&gt; tag, it gives this error:</div>





<div> </div>


<div>Traceback (most recent call last):<br>
  File &quot;C:\Users\tylersc\Desktop\Tyler2\Tyler\words_per_review_IMDB_9-13-10.py&quot;, line 168, in &lt;module&gt;<br>
    main(&quot;.&quot;,&quot;output.csv&quot;)<br>
  File &quot;C:\Users\tylersc\Desktop\Tyler2\Tyler\words_per_review_IMDB_9-13-10.py&quot;, line 166, in main<br>
    os.path.walk(top_level_dir, reviewDirectory, writer )<br>
  File &quot;C:\Python26\lib\ntpath.py&quot;, line 259, in walk<br>
    func(arg, top, names)<br>
  File &quot;C:\Users\tylersc\Desktop\Tyler2\Tyler\words_per_review_IMDB_9-13-10.py&quot;, line 162, in reviewDirectory<br>
    reviewFile( dirname+&#39;/&#39;+fileName, args )<br>
  File &quot;C:\Users\tylersc\Desktop\Tyler2\Tyler\words_per_review_IMDB_9-13-10.py&quot;, line 74, in reviewFile<div><br>
    rgenre = re.split(r&#39;;&#39;, <a href="http://rf.info" target="_blank">rf.info</a>[&quot;genre&quot;])<br></div>
KeyError: &#39;genre&#39;<br>
</div>


<div>I&#39;m about to give what may be too much information--I really thought there must be a way to say &quot;don&#39;t choke if you don&#39;t find any rgenres because <a href="http://rf.info" target="_blank">rf.info</a>[&quot;genre&quot;] was empty&quot;. But maybe I need to define the &quot;None&quot; condition earlier?</div>





<div> </div>


<div>Basically a text file has this structure:</div>


<div>&lt;info&gt;</div>


<div>&lt;title&gt;High Noon&lt;/title&gt;</div>


<div>&lt;genre&gt;Drama;Western&lt;/genre&gt; # But this tag doesn&#39;t exist for all text files</div>


<div># etc</div>


<div>&lt;/info&gt;</div>


<div>&lt;review&gt;</div>


<div>&lt;author&gt;u493498&lt;/author&gt;</div>


<div>&lt;rating&gt;9 out of 10&lt;/rating&gt;</div>


<div>&lt;summary&gt;A great flick&lt;/summary&gt;</div>


<div>&lt;text&gt;blah blah blah&lt;/text&gt;</div>


<div># etc</div>


<div>&lt;/review&gt;</div>


<div># next review--all about the movie featured in the info tags</div><div><div></div><div>


<div> </div>


<div> </div>


<div style="clear:both"></div>
<br>
<br>


<div style="font-family:arial,helvetica;color:black;font-size:10pt">-----Original Message-----<br>
From: Vince Spicer &lt;<a href="mailto:vince@vinces.ca" target="_blank">vince@vinces.ca</a>&gt;<br>
To: <a href="mailto:aeneas24@priest.com" target="_blank">aeneas24@priest.com</a><br>
Cc: <a href="mailto:tutor@python.org" target="_blank">tutor@python.org</a><br>
Sent: Mon, Sep 13, 2010 9:08 pm<br>
Subject: Re: [Tutor] If/elif/else when a list is empty<br>
<br>


<div><br>
<br>


<div class="gmail_quote">On Mon, Sep 13, 2010 at 9:58 PM, <span dir="ltr">&lt;<a href="mailto:aeneas24@priest.com" target="_blank">aeneas24@priest.com</a>&gt;</span> wrote:<br>

<blockquote style="border-left:#ccc 1px solid;margin:0px 0px 0px 0.8ex;padding-left:1ex" class="gmail_quote"><font color="black" size="2" face="arial">

<div><font face="Arial, Helvetica, sans-serif"></font>Hi,</div>


<div> </div>


<div>I&#39;m parsing IMDB movie reviews (each movie is in its own text file). In my script, I&#39;m trying to extract genre information. Movies have up to three categories of genres--but not all have a &quot;genre&quot; tag and that fact is making my script abort whenever it encounters a movie text file that doesn&#39;t have a &quot;genre&quot; tag. </div>





<div> </div>


<div>I thought the following should solve it, but it doesn&#39;t. The basic question is how I say &quot;if genre information doesn&#39;t at all, just make rg1=rg2=rg3=&quot;NA&quot;?</div>


<div> </div>


<div>rgenre = re.split(r&#39;;&#39;, <a href="http://rf.info/" target="_blank">rf.info</a>[&quot;genre&quot;]) # When movies have genre information they store it as &lt;genre&gt;Drama;Western;Thriller&lt;/genre&gt;</div>





<div> </div>


<div>if len(rgenre)&gt;0:<br>
          if len(rgenre)&gt;2:<br>
              rg1=rgenre[0]<br>
              rg2=rgenre[1]<br>
              rg3=rgenre[2]<br>
          elif len(rgenre)==2:<br>
              rg1=rgenre[0]<br>
              rg2=rgenre[1]<br>
              rg3=&quot;NA&quot;<br>
          elif len(rgenre)==1:<br>
              rg1=rgenre[0]<br>
              rg2=&quot;NA&quot;<br>
              rg3=&quot;NA&quot;<br>
   else len(rgenre)&lt;1: # I was hoping this would take care of the &quot;there is no genre information&quot; scenario but it doesn&#39;t<br>
           rg1=rg2=rg3=&quot;NA&quot;</div>


<div> </div>


<div>This probably does a weird nesting thing, but even simpler version I have tried don&#39;t work. </div>


<div> </div>


<div>Thanks very much for any help!</div>


<div> </div>


<div>Tyler<br>
      <br>
<br>
</div>


<div style="clear:both"></div>
</font><br>
_______________________________________________<br>
Tutor maillist  -  <a href="mailto:Tutor@python.org" target="_blank">Tutor@python.org</a><br>
To unsubscribe or change subscription options:<br>
<a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>
<br>
</blockquote></div>
<br>


<div>Hey Tyler you can simplify this with a onliner.</div>


<div><br>
</div>


<div>rg1, rg2, rg3 = rgenre + [&quot;NA&quot;]*(3-len(rgenre[:3]))</div>


<div><br>
</div>


<div>Hope that helps, if you have any questions feel free to ask.</div>


<div><br>
</div>


<div>Vince</div>
</div>
</div>
</div></div></font>
</blockquote></div><br></div></div>