0

我在处理这段代码时遇到了问题。尽管名称错误似乎很普遍,但我无法通过搜索找到解决方法。这是代码...

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'

和追溯....

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

这个函数是从 main() 调用的,但我不明白为什么“DataPlotLoc”是一个全局名称,因为它在这个函数中。无论哪种方式,我认为我缺少一条线来定义它,但是如何以及在哪里,我不知道。我很想得到一些帮助。

编辑:只是为了添加更多信息..'DataPlotLoc'是列表插入代码时的名称,即。DataPlotLoc=[['a', 'b', 'c',....]] 并且它起作用了。线 plotOrig = DataPlotLoc[int(in_Sel) + 1] 指的是这个列表,但显然它现在正在被 csv.reader 读入,所以现在我不确定如何分配这个变量。我假设在确认用户是否输入“q”之后,我仍然需要它接受一个整数,并且 +1 是添加到输入的数字上,以便它与从列表中选择的相应行项目的正确索引号对齐。对不起,如果这有点令人困惑,但我自己有点困惑......

4

2 回答 2

2

好吧,正如错误消息所说,您DataPlotLoc在定义它之前正在使用它。如果您搜索您的代码,您会发现它从未在任何地方定义过。在不知道您的意思的情况下,无法真正回答更多。

Python 假定您的意思是该名称的全局变量,因为您从未为它分配任何内容,这会使它成为局部变量。

于 2013-04-22T23:41:01.943 回答
0

Python 之所以这么说,是global name ... not defined因为它在函数体中看不到任何赋值DataPlotLoc,所以它假定它必须是一个全局变量,并且无法在那里找到它。(请参阅下面的 abarnert 评论)

从您的代码来看,我想您想DataPlotLoc包含从中提取的信息MonPlotDb.csv,在这种情况下,您需要做两件事:

(A) 初始化DataPlotLoc

def fmp_sel():
    DataPlotLoc = []  # <-----------------!!!!
    with open ('MonPlotDb.csv', 'rU') as csvfile:

DataPlotLoc(B)在循环和打印选项时将值附加到。

    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)

我不确定为什么要在 行中添加一个plotOrig = DataPlotLoc[int(in_Sel) + 1],并且我认为您可以将csv.reader行简化为以下内容csv.reader(csvfile)(我认为带逗号的 excel 是默认行为)

编辑:要从 CSV 的每一行中仅提取一列,请将 B 部分中的代码更改为如下所示:

    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
于 2013-04-22T23:51:57.460 回答