for循环内变量计数不增加

variable count not increasing inside for loop

for keys in dict:
        count=0
        outSheet.write(2, count+1, keys)
        print(count)
        count = count +1   

我想将字典 dict 的键复制到 excel sheet 中。 我期望 outSheet.write 函数将键复制到 excel 文件中,从坐标 (count+1 ,2) 开始,每次迭代将 y 坐标增加 1。但是 count 的值似乎并没有在每次迭代中增加。 我应该怎么做才能增加 count 的值,以便可以复制所有密钥。 谢谢

循环中的第一行将计数设置为 0,无论循环多少次,它总是会重置回 0。如果要递增,请将其移至循环外

count=0

也把你的增量改成这个更干净

count += 1 

像这样

count = 0
for keys in dict:
    outSheet.write(2, count+1, keys)
    print(count)
    count += 1