简体   繁体   English

重置csv.reader()迭代器

[英]Reset the csv.reader() iterator

I was trying to do some csv processing using csv reader and was stuck on an issue where I have to iterate over lines read by the csv reader. 我试图使用csv阅读器进行一些csv处理,但遇到了一个问题,即我必须遍历csv阅读器读取的行。 But on iterating second time, it returns nil since all the lines have already been iterated, is there any way to refresh the iterator to start from the scratch again. 但是在第二次迭代时,由于所有行都已被迭代,因此它返回nil,是否有任何方法可以刷新迭代器以从头开始。

Code: 码:

desc=open("example.csv","r")

Reader1=csv.read(desc)

for lines in Reader1:
(Some code)

for lines in Reader1:
(some code)

what is precisely want to do is read a csv file in the format below 恰恰想要做的就是读取以下格式的csv文件

id,price,name x,y,za,b,c and rearrange it in the format below id:xa price: yb name: zc without using pandas library id,price,名称x,y,za,b,c并按照id:xa price:yb name:zc的格式重新排列,无需使用熊猫库

Reset the underlying file object with seek , adding the following before the second loop: 使用seek重置基础文件对象,在第二个循环之前添加以下内容:

desc.seek(0)
# Apparently, csv.reader will not refresh if the file is seeked to 0,
# so recreate it
Reader1 = csv.reader(desc)  

Mind you, if memory is not a concern, it would typically be faster to read the input into a list , then iterate the list twice. 请注意,如果不关心内存,通常将输入读取到list ,然后对list两次迭代会更快。 Alternatively, you could use itertools.tee to make two iterators from the initial iterator (it requires similar memory to slurping to list if you iterate one iterator completely before starting the other, but allows you to begin iterating immediately, instead of waiting for the whole file to be read before you can process any of it). 或者,您可以使用itertools.tee从初始迭代器中创建两个迭代器(如果在启动另一个迭代器之前完全迭代一个迭代器,则需要消耗类似的内存来list ,但可以让您立即开始迭代,而不必等待整个迭代文件,然后才能对其进行处理)。 Either approach avoids additional system calls that iterating the file twice would entail. 每种方法都避免了额外的系统调用,该调用需要对文件进行两次迭代。 The tee approach, after the line you create Reader1 on: tee方法,在以下行创建了Reader1之后:

# It's not safe to reuse the argument to tee, so we replace it with one of
# the results of tee
Reader1, Reader2 = itertools.tee(Reader1)

for line in Reader1:
    ...

for line in Reader2:
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM