英文:
Resetting cursor in Reader
问题
我正在编写一个程序来读取CSV文件。我遇到了一个奇怪的EOF错误,想知道是否有一种方法可以将*csv.Reader
的行光标重置到文件的开头,而不需要创建一个新的读取器?
英文:
I am working on a program to read from a csv file. I am encountering a strange EOF error and was wondering if there was a way to reset the line cursor of *csv.Reader
to the beginning of the file without creating a new one?
答案1
得分: 1
如果你传递给csv.NewReader
的io.Reader
是一个io.File
,那么你可以调用Seek(0, 0)
将其重置回开头。
不过,对于CSV来说,不确定这样做会如何工作,因为解析器可能已经知道它已经读取了第一行。我猜你可以通过读取它来跳过那一行。
英文:
If the io.Reader
you are passing to csv.NewReader
is a io.File
then you can call Seek(0, 0)
on it to reset it back to the start.
Not sure how this will work with CSV though since the parser may already know that it has read the first line. I guess you could skip over that by reading it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论