ruby / csv

CSV Reading and Writing
https://ruby.github.io/csv/
BSD 2-Clause "Simplified" License
177 stars 113 forks source link

Loading line becomes strange when skip_lines is specified #296

Closed rytkmt closed 5 months ago

rytkmt commented 5 months ago

If skip_lines is not specified

Ruby3.1.3 pry(main)> require 'csv'; Tempfile.open { |t| t.write(%|"\n",""\r\n|); t.rewind; CSV.open(t.path, row_sep: "\r\n") { p _1.to_a } }
=> [["\n", ""]]

If specifying skip_lines that does not affect reading

Ruby3.1.3 pry(main)> require 'csv'; Tempfile.open { |t| t.write(%|"\n",""\r\n|); t.rewind; CSV.open(t.path, skip_lines: /xxx/, row_sep: "\r\n") { p _1.to_a } }
CSV::MalformedCSVError: Any value after quoted field isn't allowed in line 1.

Probably CSV::Parser#skip_needless_lines is the cause

kou commented 5 months ago

Could you try the latest csv gem?

rytkmt commented 5 months ago

@kou

It was the same result

Ruby3.2.3 pry(main)> require 'csv'; Tempfile.open { |t| t.write(%|"\n",""\r\n|); t.rewind; CSV.open(t.path, row_sep: "\r\n") { p _1.to_a } }
[["\n", ""]]
=> [["\n", ""]]
Ruby3.2.3 pry(main)> require 'csv'; Tempfile.open { |t| t.write(%|"\n",""\r\n|); t.rewind; CSV.open(t.path, skip_lines: /xxx/, row_sep: "\r\n") { p _1.to_a } }
CSV::MalformedCSVError: Any value after quoted field isn't allowed in line 1.
from /home/vagrant/.rbenv/versions/3.2.3/lib/ruby/3.2.0/csv/parser.rb:1067:in `parse_quotable_robust'
kou commented 5 months ago

If CSV::VERSION isn't "3.2.8", you're not using the latest csv gem.

rytkmt commented 5 months ago

@kou Sorry, I was wrong.

but the result was the same.

Ruby3.2.3 pry(main)> require 'csv'; Tempfile.open { |t| t.write(%|"\n",""\r\n|); t.rewind; CSV.open(t.path, row_sep: "\r\n") { p _1.to_a } }
[["\n", ""]]
=> [["\n", ""]]
Ruby3.2.3 pry(main)> require 'csv'; Tempfile.open { |t| t.write(%|"\n",""\r\n|); t.rewind; CSV.open(t.path, skip_lines: /xxx/, row_sep: "\r\n") { p _1.to_a } }
CSV::MalformedCSVError: Any value after quoted field isn't allowed in line 1.
from /home/vagrant/.rbenv/versions/3.2.3/lib/ruby/gems/3.2.0/gems/csv-3.2.8/lib/csv/parser.rb:1066:in `parse_quotable_robust'
Ruby3.2.3 pry(main)> CSV::VERSION
=> "3.2.8"
kou commented 5 months ago

Thanks for checking.

I've fixed this and released 3.2.9. Could you try it?

rytkmt commented 4 months ago

@kou Sorry for the late confirmation. I can confirm that it works properly. Thank you for your quick response.