将dict值分配给变量时出现问题

PyN*_*bie -1 python csv

我正在努力做一些必须是其中之一的事情'这是显而易见的我是一个白痴'的问题.我有一个csv文件,我想读入并用于创建单独的"表".我有一个变量(RID),标志着一个新的'表'的开始.

当我完成操作每一行时,我无法使我的指标变量(currentRow)前进.你可以看到print语句,currentRow保持等于0.
但是如果我在循环之外使用赋值语句,我可以随意改变currentRow的值.测试任务只是了解我在循环中的位置.

currentRow=0
test=0
theTables=defaultdict(list)
for line in csv.DictReader(open(r'c:\temp\testread.csv')):
    newTableKey=line['CIK']+'-'+line['RDATE']+'-'+line['CDATE']+'-'+line['FNAME']+' '+line['TID']

    if line['RID']=='1':
        test+=1 # I can get here
        if currentRow>int(line['RID']):
            print 'got here'

            theTables[oldTableKey]=theList
            test+=1  # I cannot get here
        theList=[]
    theList.append(line)
    currrentRow=int(line['RID'])
    print currentRow  #this value always prints at 0
    print int(line['RID']) #this prints at the correct value
    oldTableKey=newTableKey
Run Code Online (Sandbox Code Playgroud)

Ale*_*lli 7

在线:

currrentRow=int(line['RID'])
Run Code Online (Sandbox Code Playgroud)

你有3 r以s currrentRow.将它们减少到两个,事情应该改善.

  • 并在代码上运行pylint或类似工具,这样您就不必每次都要求Alex检查您的代码.;-)见http://infinitemonkeycorps.net/docs/pph/#id3 (2认同)