Golang CSV读取:字段中存在多余的引号错误。

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

Golang CSV read : extraneous " in field error

问题

我正在使用一个简单的程序来读取CSV文件,但是我注意到当我使用EXCEL或基于Windows的计算机创建CSV文件时,Go库无法正确读取它。即使我使用cat命令,在终端上也只显示最后一行。它总是报错extraneous " in field

我进行了一些研究,发现这与操作系统之间的回车符差异有关。但我真的想知道如何创建一个通用的CSV读取器。我尝试使用pandas读取相同的CSV文件,它可以成功读取。但是我无法通过我的Go代码实现这一点。

此外,这是正确的CSV文件的屏幕截图:

Golang CSV读取:字段中存在多余的引号错误。

英文:

I am using a simple program to read CSV file, somehow I noticed when I created a CSV using EXCEL or windows based computer go library fails to read it. even when I use cat command it only shows me last line on the terminal. It always results in this error extraneous " in field.

I researched somewhat than I found it is somewhat related to carriage return differences between OS.
But I really want to ask how to make a generic csv reader. I tried reading the same csv using pandas and it was reading successfully. But i am not been able to achieve this using my Go code.

Golang CSV读取:字段中存在多余的引号错误。

Also screen shot of correct csv Is here

Golang CSV读取:字段中存在多余的引号错误。

答案1

得分: 1

你的文件明显显示在内容的末尾多了一个引号。虽然像pandas这样的程序可能可以处理这种情况,但我认为这对于csv来说是无效的,所以Go会返回一个错误。

以下是你的数据有问题的一个快速示例:https://play.golang.org/p/KBikSc1nzD

更新:在你的更新和一些搜索之后,我不得不道歉,回车符确实很重要,似乎是主要的问题所在,Go似乎可以处理\r\n的Windows变体,但不能处理\r。在这种情况下,你可以将bytes.Reader包装在一个自定义的读取器中,将\r字节替换为\n字节。

以下是一个示例:https://play.golang.org/p/vNjzwAHmtg

请注意,这个示例只是一个示例,它并没有处理所有可能出现合法\r字节的情况。

英文:

Your file clearly shows that you've got an extra quote at the end of the content. While programs like pandas may be fine with that, I assume it's not valid csv so go does return an error.

Quick example of what's wrong with your data: https://play.golang.org/p/KBikSc1nzD

Update: After your update and a little bit of searching, I have to apoligize, the carriage return does matter and seems to be tha main culprit here, Go seems to be ok handling the \r\n windows variant but not the \r one. In that case what you can do is wrap the bytes.Reader into a custom reader that replaces the \r byte with the \n byte.

Here's an example: https://play.golang.org/p/vNjzwAHmtg

Please note, that the example is just that, an example, it's not handling all the possible cases where \r might be a legit byte.

huangapple
  • 本文由 发表于 2017年4月5日 02:05:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/43214523.html
匿名

发表评论

匿名网友

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

确定