Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 迭代CSV时跳过For循环_Python_Python 3.x_Csv_Csvreader - Fatal编程技术网

Python 迭代CSV时跳过For循环

Python 迭代CSV时跳过For循环,python,python-3.x,csv,csvreader,Python,Python 3.x,Csv,Csvreader,所以我有一个程序,可以读取一个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_

所以我有一个程序,可以读取一个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\u input\u stream
将以“write”模式打开。这可能已经覆盖了文件中的数据,留下了一个空白文件,这可以解释为什么for循环被“跳过”——没有数据可以迭代


你能验证一下
myinput\u stream
中是否还有实际的数据吗?

也许
myinput\u stream
是一个空文件?啊,我觉得太傻了,这就是发生的事情。当我去看CSV的时候,它已经变成了一个空的文本文件。这发生在我们当中最好的人身上!