[Tutor] If/elif/else when a list is empty
vince at vinces.ca
Tue Sep 14 06:56:38 CEST 2010
This is a simple error the KeyError is caused because the key isn't in the
http://docs.python.org/library/exceptions.html#exceptions.KeyError and easy
fix you can either check for the key prior to using or use the get method
1) rgenre = re.split(r';', rf.info["genre"]) if "genre" in rf.info else 
the regex will never get executed here and an empty list will be returned if
genre not present
2) rgenre = re.split(r';', rf.info.get("genre", ''))
the get will not throw this error and return '' if ''genre' is not found in
Hope that helps,
On Mon, Sep 13, 2010 at 10:46 PM, <aeneas24 at priest.com> wrote:
> Hi Vince,
> 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:
> Traceback (most recent call last):
> line 168, in <module>
> line 166, in main
> os.path.walk(top_level_dir, reviewDirectory, writer )
> File "C:\Python26\lib\ntpath.py", line 259, in walk
> func(arg, top, names)
> line 162, in reviewDirectory
> reviewFile( dirname+'/'+fileName, args )
> line 74, in reviewFile
> rgenre = re.split(r';', rf.info["genre"])
> KeyError: 'genre'
> 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?
> Basically a text file has this structure:
> <title>High Noon</title>
> <genre>Drama;Western</genre> # But this tag doesn't exist for all text
> # etc
> <rating>9 out of 10</rating>
> <summary>A great flick</summary>
> <text>blah blah blah</text>
> # etc
> # next review--all about the movie featured in the info tags
> -----Original Message-----
> From: Vince Spicer <vince at vinces.ca>
> To: aeneas24 at priest.com
> Cc: tutor at python.org
> Sent: Mon, Sep 13, 2010 9:08 pm
> Subject: Re: [Tutor] If/elif/else when a list is empty
> On Mon, Sep 13, 2010 at 9:58 PM, <aeneas24 at priest.com> wrote:
>> 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
>> 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:
>> elif len(rgenre)==2:
>> elif len(rgenre)==1:
>> else len(rgenre)<1: # I was hoping this would take care of the "there
>> is no genre information" scenario but it doesn't
>> This probably does a weird nesting thing, but even simpler version I have
>> tried don't work.
>> Thanks very much for any help!
>> Tutor maillist - Tutor at python.org
>> To unsubscribe or change subscription options:
> Hey Tyler you can simplify this with a onliner.
> rg1, rg2, rg3 = rgenre + ["NA"]*(3-len(rgenre[:3]))
> Hope that helps, if you have any questions feel free to ask.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Tutor