<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<br>
rgenre = re.split(r';', rf.info["genre"])<br>
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 rf.info["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 style="CLEAR: both"></div>
<br>
<br>
<div style="FONT-FAMILY: arial,helvetica; COLOR: black; FONT-SIZE: 10pt">-----Original Message-----<br>
From: Vince Spicer <vince@vinces.ca><br>
To: aeneas24@priest.com<br>
Cc: tutor@python.org<br>
Sent: Mon, Sep 13, 2010 9:08 pm<br>
Subject: Re: [Tutor] If/elif/else when a list is empty<br>
<br>
<div id=AOLMsgPart_2_ca8a283f-05be-4304-8a15-f7f0396315ca><br>
<br>
<div class=gmail_quote>On Mon, Sep 13, 2010 at 9:58 PM, <SPAN dir=ltr><<A href="mailto:aeneas24@priest.com">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">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>
<!-- end of AOLMsgPart_2_ca8a283f-05be-4304-8a15-f7f0396315ca --></div>
</font>