Bug #15388
closedCSV parsing behaviour in > 2.5.0
Description
Hi,
It seems like CSV library in 2.5.0 is not behaving as intended while parsing.
header = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q"]
csv_str = "10.4710|859.5170|9000.0000|Y||0.0000||NRM|0||||N|Y|P|NRM|\r\n11.4710|869.5170|9000.0000|Y||0.0000||NRM|0||||N|Y|P|NRM|\r\n"
CSV.parse(csv_str, col_sep: '|', write_headers: true, headers: header)[0]
# produces junk
# #<CSV::Row "A":"0" "B":nil "C":"NRM" "D":"0" "E":nil "F":nil "G":nil "H":"N" "I":"Y" "J":"P" "K":"NRM" "L":nil "M":nil "N":nil "O":nil "P":nil "Q":nil>
But without write_headers
option it produces correct parsing.
CSV.parse(csv_str, col_sep: '|', headers: header)[0]
# #<CSV::Row "A":"10.4710" "B":"859.5170" "C":"9000.0000" "D":"Y" "E":nil "F":"0.0000" "G":nil "H":"NRM" "I":"0" "J":nil "K":nil "L":nil "M":"N" "N":"Y" "O":"P" "P":"NRM" "Q":nil>
Updated by nobu (Nobuyoshi Nakada) over 5 years ago
- Description updated (diff)
Why do you want to write headers when parsing?
Updated by nobu (Nobuyoshi Nakada) over 5 years ago
Maybe an error is expected?
diff --git i/lib/csv.rb w/lib/csv.rb
index dca2a45b6a..e4ad55085c 100644
--- i/lib/csv.rb
+++ w/lib/csv.rb
@@ -687,8 +687,9 @@
# You pass your +str+ to read from, and an optional +options+ containing
# anything CSV::new() understands.
#
- def self.parse(*args, &block)
- csv = new(*args)
+ def self.parse(data, write_headers: nil, **options, &block)
+ raise ArgumentError.new("Cannot write headers when parsing") if write_headers
+ csv = new(data, **options)
return csv.each(&block) if block_given?
@@ -707,7 +708,8 @@
#
# The +options+ parameter can be anything CSV::new() understands.
#
- def self.parse_line(line, **options)
+ def self.parse_line(line, write_headers: nil, **options)
+ raise ArgumentError.new("Cannot write headers when parsing") if write_headers
new(line, options).shift
end
diff --git i/test/csv/test_csv_parsing.rb w/test/csv/test_csv_parsing.rb
index e65bbad92e..7e45a429ef 100755
--- i/test/csv/test_csv_parsing.rb
+++ w/test/csv/test_csv_parsing.rb
@@ -239,6 +239,29 @@
CSV.parse("a b d", col_sep: " "))
end
+ def test_parse_write_headers
+ data = "a,b,c"
+ headers = %w[A B C]
+ Tempfile.create(%w[parsing .csv]) do |f|
+ f.puts data
+ f.close
+ File.open(f.path) {|f|
+ assert_raise(ArgumentError) {
+ CSV.parse(f, write_headers: true, headers: headers)
+ }
+ assert_raise(ArgumentError) {
+ CSV.parse_line(f, write_headers: true, headers: headers)
+ }
+ }
+ end
+ assert_raise(ArgumentError) {
+ CSV.parse(data, write_headers: true, headers: headers)
+ }
+ assert_raise(ArgumentError) {
+ CSV.parse_line(data, write_headers: true, headers: headers)
+ }
+ end
+
private
def assert_parse_errors_out(*args)
Updated by dharshandj@gmail.com (Dharshan Bharathuru) over 5 years ago
nobu (Nobuyoshi Nakada) wrote:
Why do you want to write headers when parsing?
I needed a object-key reference in future references instead of index based references.
Updated by nobu (Nobuyoshi Nakada) over 5 years ago
dharshandj@gmail.com (Dharshan Bharathuru) wrote:
I needed a object-key reference in future references instead of index based references.
It doesn't need to write headers.
Updated by kou (Kouhei Sutou) over 5 years ago
It seems that this has been fixed in csv master.
I've released csv gem 3.0.1. Can you try it?
Updated by kou (Kouhei Sutou) over 5 years ago
- Status changed from Open to Assigned
- Assignee set to kou (Kouhei Sutou)
Arguments validation may be better but we need more works for it. For example, we need to add similar logic to CSV.read
for this case.
Anyone who wants to work on argument validation should send pull requests to https://github.com/ruby/csv .
Updated by dharshandj@gmail.com (Dharshan Bharathuru) over 5 years ago
kou (Kouhei Sutou) wrote:
It seems that this has been fixed in csv master.
I've released csv gem 3.0.1. Can you try it?
Thanks. Working fine with version 3.0.1.
Updated by kou (Kouhei Sutou) over 5 years ago
- Status changed from Assigned to Closed
OK. I'll close this.