Foreach循环忽略第一个对象

时间:2018-06-12 02:39:25

标签: c# foreach

我想用我找到的物品之后做一些东西。出于测试目的,我正在更改名称。但结果如下:

  • 删除
  • OLDNAME
  • 2
  • 3

第一个(删除后)并未受到影响。有人能帮助我吗?

import csv

divisor = 500000

outfileno = 1
outfile = None

with open('testM.txt', 'r') as infile:
    infile_iter = csv.reader(infile)
    header = next(infile_iter)
    for index, row in enumerate(infile_iter):
        if index % divisor == 0:
            if outfile is not None:
                outfile.close()
            outfilename = 'big-{}.csv'.format(outfileno)
            outfile = open(outfilename, 'w')
            outfileno += 1
            writer = csv.writer(outfile)
            writer.writerow(header)
        writer.writerow(row)

    if outfile is not None:
        outfile.close()

1 个答案:

答案 0 :(得分:-1)

  1. 您不能从foreach案例中删除pnConacts.Controls - 非静态字段中的项目,并尝试在静态上下文中使用它;
  2. rUser.DisplayName = i.ToString();仅在foreach上下文中更改名称
  3. UPD:在你的情况下使用简单的循环for是更好的方法,你需要在按钮名称中添加索引