[Tutor] If/elif/else when a list is empty
aeneas24 at priest.com
aeneas24 at priest.com
Tue Sep 14 06:46:36 CEST 2010
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):
File "C:\Users\tylersc\Desktop\Tyler2\Tyler\words_per_review_IMDB_9-13-10.py", line 168, in <module>
File "C:\Users\tylersc\Desktop\Tyler2\Tyler\words_per_review_IMDB_9-13-10.py", 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)
File "C:\Users\tylersc\Desktop\Tyler2\Tyler\words_per_review_IMDB_9-13-10.py", line 162, in reviewDirectory
reviewFile( dirname+'/'+fileName, args )
File "C:\Users\tylersc\Desktop\Tyler2\Tyler\words_per_review_IMDB_9-13-10.py", line 74, in reviewFile
rgenre = re.split(r';', rf.info["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:
<genre>Drama;Western</genre> # But this tag doesn't exist for all text files
<rating>9 out of 10</rating>
<summary>A great flick</summary>
<text>blah blah blah</text>
# next review--all about the movie featured in the info tags
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 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>
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