<div>Tyler,</div><div><br></div><div>This is a simple error the KeyError is caused because the key isn'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';', <a href="http://rf.info">rf.info</a>["genre"]) if "genre" 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';', rf.info.get("genre", ''))</div><div>the get will not throw this error and return '' if ''genre' 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"><<a href="mailto:aeneas24@priest.com" target="_blank">aeneas24@priest.com</a>></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 <genre> tag, it gives this error:</div>
<div> </div>
<div>Traceback (most recent call last):<br>
File "C:\Users\tylersc\Desktop\Tyler2\Tyler\words_per_review_IMDB_9-13-10.py", line 168, in <module><br>
main(".","output.csv")<br>
File "C:\Users\tylersc\Desktop\Tyler2\Tyler\words_per_review_IMDB_9-13-10.py", line 166, in main<br>
os.path.walk(top_level_dir, reviewDirectory, writer )<br>
File "C:\Python26\lib\ntpath.py", line 259, in walk<br>
func(arg, top, names)<br>
File "C:\Users\tylersc\Desktop\Tyler2\Tyler\words_per_review_IMDB_9-13-10.py", line 162, in reviewDirectory<br>
reviewFile( dirname+'/'+fileName, args )<br>
File "C:\Users\tylersc\Desktop\Tyler2\Tyler\words_per_review_IMDB_9-13-10.py", line 74, in reviewFile<div><br>
rgenre = re.split(r';', <a href="http://rf.info" target="_blank">rf.info</a>["genre"])<br></div>
KeyError: 'genre'<br>
</div>
<div>I'm about to give what may be too much information--I really thought there must be a way to say "don't choke if you don't find any rgenres because <a href="http://rf.info" target="_blank">rf.info</a>["genre"] was empty". But maybe I need to define the "None" condition earlier?</div>
<div> </div>
<div>Basically a text file has this structure:</div>
<div><info></div>
<div><title>High Noon</title></div>
<div><genre>Drama;Western</genre> # But this tag doesn't exist for all text files</div>
<div># etc</div>
<div></info></div>
<div><review></div>
<div><author>u493498</author></div>
<div><rating>9 out of 10</rating></div>
<div><summary>A great flick</summary></div>
<div><text>blah blah blah</text></div>
<div># etc</div>
<div></review></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 <<a href="mailto:vince@vinces.ca" target="_blank">vince@vinces.ca</a>><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"><<a href="mailto:aeneas24@priest.com" target="_blank">aeneas24@priest.com</a>></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'm parsing IMDB movie reviews (each movie is in its own text file). In my script, I'm trying to extract genre information. Movies have up to three categories of genres--but not all have a "genre" tag and that fact is making my script abort whenever it encounters a movie text file that doesn't have a "genre" tag. </div>
<div> </div>
<div>I thought the following should solve it, but it doesn't. The basic question is how I say "if genre information doesn't at all, just make rg1=rg2=rg3="NA"?</div>
<div> </div>
<div>rgenre = re.split(r';', <a href="http://rf.info/" target="_blank">rf.info</a>["genre"]) # When movies have genre information they store it as <genre>Drama;Western;Thriller</genre></div>
<div> </div>
<div>if len(rgenre)>0:<br>
if len(rgenre)>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="NA"<br>
elif len(rgenre)==1:<br>
rg1=rgenre[0]<br>
rg2="NA"<br>
rg3="NA"<br>
else len(rgenre)<1: # I was hoping this would take care of the "there is no genre information" scenario but it doesn't<br>
rg1=rg2=rg3="NA"</div>
<div> </div>
<div>This probably does a weird nesting thing, but even simpler version I have tried don'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 + ["NA"]*(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>