遍历 CSV 时跳过 For 循环

For loop is skipped when iterating through CSV

所以我有一个程序可以读取 CSV 文件并在将数据输出到单独的文件之前对数据执行一些计算。昨天它运行良好,但是当我回来时,程序终止而没有调用遍历 CSV 每一行的 for 循环。没有报错。有人知道这是什么原因吗?

下面是函数。

def my_map(my_input_stream, my_output_stream, my_mapper_input_parameters):
    #files = glob.glob(my_input_stream)
    #f = open(my_input_stream,"w")
    out = codecs.open(my_output_stream,"w")
    with open(my_input_stream) as csv_file:
        csv_reader = csv.reader(csv_file)
        for line in csv_reader:
            stop_station = line[7]
            start_station = line[3]
            print(start_station)
            out.write(f"{start_station}\t(1,0)\n")
            print("Writing to file..")
            out.write(f"{stop_station}\t(0,1)\n")
    out.close()

除了 for 循环外,一切正常。

您注释掉的其中一行是

f = open(my_input_stream,"w")

如果您 运行 这样做,那么 my_input_stream 将以“写入”模式打开。这可能已经覆盖了文件中的数据并留下一个空白文件,这可以解释为什么 for 循环被“跳过”——没有数据可以迭代。

你能确认 my_input_stream 里面还有实际数据吗?