Reputation: 3920
UPDATE: In response to Wooble's comment, adding a "sector = None" before the for simply returns "None". I think the issue is that the variable in the for loop is not being returned.
The following is part of a function that was running fine, until recently, when I changed a seemingly unrelated part of the code.
#--> the only part I had changed recently was adding "stockurl" to the return statement
I now get UnboundLocalError: local variable "sector" referenced before assignment, referring to the "return" line
for sec in root.xpath(r'''//a[re:match(@href, "http://biz.yahoo.com/p/[0-9]{1}conameu.html")]''',
namespaces={'re': 'http://exslt.org/regular-expressions'}):
sector = sec.text
#print "Sector: " + sector
for ind in root.xpath(r'''//a[re:match(@href, "http://biz.yahoo.com/ic/[0-9]{1,9}.html")]''',
namespaces={'re': 'http://exslt.org/regular-expressions'}):
industry = ind.text
#print "Industry: " + industry
#index --> don't populate here
#followers --> don't populate here
return a, b, c, d, e, f, stockurl, sector, industry
#--> the only part I had changed recently was adding "stockurl" to the function
Upvotes: 0
Views: 8896
Reputation: 613302
Let's take a look at the error message and work backwards:
local variable "sector" referenced before assignment
That means that you are referring to sector
but that sector
has not been assigned, or bound, to an object.
The only assignment to sector
is inside the body of the for loop
. So, clearly, the body of the for
loop was not entered. And this can only happen if the call to root.xpath()
returns an iterable object that is empty.
Upvotes: 5