重置阅读器中的光标。

huangapple go评论76阅读模式
英文:

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.NewReaderio.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.

huangapple
  • 本文由 发表于 2015年8月3日 02:27:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/31775184.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定