[Tutor] If/elif/else when a list is empty

aeneas24 at priest.com aeneas24 at priest.com
Tue Sep 14 05:58:07 CEST 2010


Hi,

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. 

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"?

rgenre = re.split(r';', rf.info["genre"]) # When movies have genre information they store it as <genre>Drama;Western;Thriller</genre>

if len(rgenre)>0:
          if len(rgenre)>2:
              rg1=rgenre[0]
              rg2=rgenre[1]
              rg3=rgenre[2]
          elif len(rgenre)==2:
              rg1=rgenre[0]
              rg2=rgenre[1]
              rg3="NA"
          elif len(rgenre)==1:
              rg1=rgenre[0]
              rg2="NA"
              rg3="NA"
   else len(rgenre)<1: # I was hoping this would take care of the "there is no genre information" scenario but it doesn't
           rg1=rg2=rg3="NA"

This probably does a weird nesting thing, but even simpler version I have tried don't work. 

Thanks very much for any help!

Tyler
      



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100913/b4b1efc3/attachment.html>


More information about the Tutor mailing list