0

它已经非常接近于做我想做的事情了,这让用户通过它的索引号(0-11)在列表中选择一个项目。

当我输入一个数字,即 5 时,它会打印“ You selected [5] as origin point"..great!但我希望它显示绘图的名称,而不是索引号。名称在第 2 列中(如果索引列计为 0) .我意识到它要求整数,但我希望它很容易修复。

另一个问题是如果我输入 18,它会打印"You selected [18]...",但没有 18。

def fmp_sel():
    DataPlotLoc= file('MonPlotDb.csv', 'rU')
    fmpList = csv.reader(DataPlotLoc, dialect=csv.excel_tab)
    next(fmpList, None)
    for item in enumerate(fmpList):
        print "[%d] %s" % (item)

    while True:
        try:
            in_Sel = raw_input(('''Choose a plot 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

            in_Sel = DataPlotLoc[int(in_Sel) + 1] # The +1 is to align to correct index
            print 'You selected', in_Sel, 'as origin point'
            break

        except (ValueError, IndexError):
            print 'Error: Try again'
4

1 回答 1

0

你需要 fmpList 到一个列表,csv阅读器只会循环一次,

您也可以将代码更改为

DataPlotLoc= file('MonPlotDb.csv', 'rU')
fmpList = list(csv.reader(DataPlotLoc, dialect=csv.excel_tab))
for item in enumerate(fmpList):
   print "[%d] %s" % (item)
if fmpList:
   while True:
         try:
            in_Sel = raw_input(('''Choose a plot from list, or 'q' to quit: '''))
            in_Sel = DataPlotLoc[int(in_Sel)][1] # The +1 is to align to correct index
            print 'You selected', in_Sel, 'as origin point'
            break    
        except (ValueError, IndexError):
            print 'Error: Try again'
于 2013-04-23T10:30:18.763 回答