使用FasterCSV gem克服CSV解析的基本问题 [英] Overcoming a basic problem with CSV parsing using the FasterCSV gem

查看:101
本文介绍了使用FasterCSV gem克服CSV解析的基本问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现FasterCSV(1.5.0)出现了CSV解析问题,这似乎是一个真正的错误,但我希望有一个解决方法.

I have found a CSV parsing issue with FasterCSV (1.5.0) which seems like a genuine bug, but which I'm hoping there's a workaround for.

基本上,当字段用引号引起来时,在分隔符(在我的情况下为逗号)之后添加空格会生成MalformedCSVError.

Basically, adding a space after the separator (in my case a comma) when the fields are enclosed in quotes generates a MalformedCSVError.

这是一个简单的例子:

# No quotes on fields -- works fine
FasterCSV.parse_line("one,two,three")
=> ["one", "two", "three"]

# Quotes around fields with no spaces after separators -- works fine
FasterCSV.parse_line("\"one\",\"two\",\"three\"")
=> ["one", "two", "three"]

# Quotes around fields but with a space after the first separator -- fails!
FasterCSV.parse_line("\"one\", \"two\",\"three\"")
=> FasterCSV::MalformedCSVError: Illegal quoting on line 1.

我会生气吗,或者这是FasterCSV中的错误吗?

Am I going mad, or is this a bug in FasterCSV?

推荐答案

MalformedCSVError在此处正确.

不会忽略CSV格式的前导/尾随空格,它们被视为字段的一部分.因此,这意味着您已经在一个带空格的字段开始,然后在该字段中包含未转义的双引号,这将导致非法的引用错误.

Leading/trailing spaces in CSV format are not ignored, they are considered part of a field. So this means you have started a field with a space, and then included unescaped double quotes in that field, which would cause the illegal quoting error.

也许这个库比您使用过的其他库更严格.

Maybe this library is just more strict than others you have used.

这篇关于使用FasterCSV gem克服CSV解析的基本问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆