Reputation: 81
I'm having trouble with this code. Although name errors seem to be prevalent, I couldn't find a fix by searching. Here's the code...
def fmp_sel():
with open ('MonPlotDb.csv', 'rU') as csvfile:
next(csvfile, None)
fmpList = csv.reader(csvfile, delimiter=',', dialect=csv.excel_tab)
for item in enumerate(fmpList):
print "[%d] %s" % (item)
while True:
try:
in_Sel = raw_input(('''Choose from list or 'q' to quit:'''))
if in_Sel == 'q':
print 'Quit?'
conf = raw_input('Really? (y or n)...')
if conf == 'y':
print 'Seeya!'
break
else:
continue
plotOrig = DataPlotLoc[int(in_Sel) + 1]
print 'You selected', plotOrig[1]
break
except (ValueError, IndexError):
print 'Error: Try again'
and the traceback....
File "E:\FireRegDb\Rec_2012\dblist_outonly.py", line 28, in fmp_sel
plotOrig = DataPlotLoc[int(in_Sel) + 1]
NameError: global name 'DataPlotLoc' is not defined
This function is being called from main() but I can't see why 'DataPlotLoc' is a global name as it's within this function. Either way, I think I'm missing a line to define it but how and where, I don't know. I would love some help.
EDIT: Just to add some more info..'DataPlotLoc' was the name of the list when it was inserted into the code ie. DataPlotLoc=[['a', 'b', 'c',....]] and it worked. The line plotOrig = DataPlotLoc[int(in_Sel) + 1] refers to this list, but obviously it's now being read in by csv.reader so now I'm not sure how to assign this variable. I assumed I still need it to accept an integer after confirming if the user enters 'q' or not and the +1 is to add to the number entered so it aligns with the correct index number for the corresponding row item selected from the list. Sorry if this is a bit confusing, but I'm a bit confused myself...
Upvotes: 0
Views: 2761
Reputation: 3149
Python is saying global name ... not defined
because it doesn't see any assignment to DataPlotLoc
inthe function body, so it assumes it must be a global variable, and fails to find it there. (See abarnert's comment below)
Judging from your code, I imagine you want DataPlotLoc
to contain the information you extract from MonPlotDb.csv
, in which case you need to do two things:
(A) Initialize DataPlotLoc
def fmp_sel():
DataPlotLoc = [] # <-----------------!!!!
with open ('MonPlotDb.csv', 'rU') as csvfile:
(B) Append the values to DataPlotLoc
while you're looping and printing the options.
next(csvfile, None)
fmpList = csv.reader(csvfile, delimiter=',', dialect=csv.excel_tab)
for item in enumerate(fmpList):
DataPlotLoc.append(item[1]) # <---------!!!
print "[%d] %s" % (item)
I'm not sure why you add one in the line for plotOrig = DataPlotLoc[int(in_Sel) + 1]
, and I think you may be able to simplify your csv.reader
line to the following csv.reader(csvfile)
(I think excel with commas is the default behavior)
Edit: To extract just one column from each row of the CSV change the code in part B to something like the following:
next(csvfile, None)
fmpList = csv.reader(csvfile, delimiter=',', dialect=csv.excel_tab)
for item in enumerate(fmpList):
i, data = item # enumerate returns tuples of the form (i, data)
vals = (i, data[1]) # <----- put in the proper index of the column
DataPlotLoc.append(vals)
print "[%d] %s" % vals # <--- assuming you want to change the print as well
Upvotes: 0
Reputation: 184191
Well, as the error message says, you're using DataPlotLoc
before defining it. If you search your code you'll see it's never defined anywhere. Can't really answer more than that without knowing what you mean it to be.
Python assumes you meant the global variable of that name because you never assigned anything to it, which would have made it a local variable.
Upvotes: 2