英文:
Golang CSV read : extraneous " in field error
问题
我正在使用一个简单的程序来读取CSV文件,但是我注意到当我使用EXCEL或基于Windows的计算机创建CSV文件时,Go库无法正确读取它。即使我使用cat命令,在终端上也只显示最后一行。它总是报错extraneous " in field
。
我进行了一些研究,发现这与操作系统之间的回车符差异有关。但我真的想知道如何创建一个通用的CSV读取器。我尝试使用pandas
读取相同的CSV文件,它可以成功读取。但是我无法通过我的Go代码实现这一点。
此外,这是正确的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.
Also screen shot of correct csv Is here
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论