我正在为一些事情而挣扎,那一定是那些“很明显我是个白痴”的问题。我有一个csv文件,我想读入并用来创建单独的“表”。我有一个变量(RID),它标记了一个新的“table”的开始。在
当我完成对每一行的操作时,我无法使指示器变量(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
在线:
在
currrentRow
中有三个r
。把他们减少到两个,情况就会好转。在一种解决方案是在循环中添加以下内容:
但是,更好的解决方案可能是使用可枚举的:
^{pr2}$在我看来,
theTables[oldTableKey]=theList
可以用未初始化的值oldTableKey
来执行。在相关问题 更多 >
编程相关推荐