重置csv.reader()迭代器 [英] Reset the csv.reader() iterator
问题描述
我正在尝试使用csv阅读器进行一些csv处理,并且陷入了一个问题,即我必须遍历csv阅读器所读取的行。但是在第二次迭代时,由于所有行都已经被迭代,因此它返回nil,是否有任何方法可以刷新迭代器以从头开始。
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. 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.
代码:
desc=open("example.csv","r")
Reader1=csv.read(desc)
for lines in Reader1:
(Some code)
for lines in Reader1:
(some code)
精确地要做的是读取以下格式的csv文件
what is precisely want to do is read a csv file in the format below
id,price,name
x,y,z
a,b,c
并按照
id以下的格式重新排列id:xa
price: yb
名称:zc
,不使用熊猫库
id,price,name x,y,z a,b,c and rearrange it in the format below id:x a price: y b name: z c without using pandas library
推荐答案
使用<$ c重置基础文件对象$ c> seek ,在第二个循环之前添加以下内容:
Reset the underlying file object with seek
, adding the following before the second loop:
desc.seek(0)
# Apparently, csv.reader will not refresh if the file is seeked to 0,
# so recreate it
Reader1 = csv.reader(desc)
提醒您,如果不考虑内存问题,通常将输入读取到列表
中,然后迭代列表
两次。或者,您可以使用 itertools.tee
从初始迭代器中创建两个迭代器(如果您在启动另一个迭代器之前完全对其进行迭代,则它需要类似的内存来吸引到 list
,但可以让您立即开始迭代,而无需等待读取整个文件再处理任何文件。两种方法都避免了额外的系统调用,该调用需要对文件进行两次迭代。 tee
方法,在您在以下位置创建 Reader1
的行之后:
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. 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). Either approach avoids additional system calls that iterating the file twice would entail. The tee
approach, after the line you create Reader1
on:
# 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:
...
这篇关于重置csv.reader()迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!