gpt4 book ai didi

Ruby CSV.parse 遇到引号时很挑剔

转载 作者:数据小太阳 更新时间:2023-10-29 08:52:16 24 4
gpt4 key购买 nike

我发现 Ruby 1.9.3 中的 CSV 解析非常脆弱。如此之多以至于我想知道我是否做错了什么

如果我在 irb 中执行以下操作,我会收到错误消息:

1.9.3-p125 :011 > require 'csv'
=> true
1.9.3-p125 :012 > a = 'one,two,three, "four, five",six'
=> "one,two,three, \"four, five\",six"
1.9.3-p125 :013 > arr = CSV.parse(a)
CSV::MalformedCSVError: Illegal quoting in line 1.
from /Users/disaacs/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/csv.rb:1925:in `block (2 levels) in shift'
from /Users/disaacs/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/csv.rb:1887:in `each'
from /Users/disaacs/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/csv.rb:1887:in `block in shift'
from /Users/disaacs/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/csv.rb:1849:in `loop'
from /Users/disaacs/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/csv.rb:1849:in `shift'
from /Users/disaacs/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/csv.rb:1791:in `each'
from /Users/disaacs/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/csv.rb:1805:in `to_a'
from /Users/disaacs/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/csv.rb:1805:in `read'
from /Users/disaacs/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/csv.rb:1379:in `parse'
from (irb):13
from /Users/disaacs/.rvm/rubies/ruby-1.9.3-p125/bin/irb:16:in `<main>'

我发现问题出在“四、五”值之前的额外空格。如果我删除空格,那么它会起作用。

1.9.3-p125 :010 > a = 'one,two,three,"four, five",six'
=> "one,two,three,\"four, five\",six"
1.9.3-p125 :011 > arr = CSV.parse(a)
=> [["one", "two", "three", "four, five", "six"]]

其他值前面的空格不会造成问题。下面的解析就好了

one, two, three,"four, five", six

是否有一些我缺少的解析选项使得使用引号的值变得如此脆弱?

最佳答案

这是正确的行为。它并不脆弱。

“四”后的逗号结束该字段,下一个字段立即以空格开始。

您不能在字段中间有效地放置引号(不转义)。

关于Ruby CSV.parse 遇到引号时很挑剔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9965838/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com