[ruby-core:80001] [Ruby trunk Bug#8784] CSV - Empty fields are discarded when col_sep is a space

From: slavcho@...
Date: 2017-03-10 15:12:10 UTC
List: ruby-core #80001
Issue #8784 has been updated by Slavcho Ivanov.


The problem is in the String::split function.
It turns out it is a feature, not a bug: http://ruby-doc.org/core-2.4.0/String.html#method-i-split .
"If pattern is a single space, str is split on whitespace, with leading whitespace and runs of contiguous whitespace characters ignored."

This should fix the problem in csv.rb - line 1834:

<<<
parts =  parse.split(@col_sep, -1)
===
sep = @col_sep == ' ' ? / / : @col_sep
parts =  parse.split(sep, -1)
>>>

Hope this helps.

----------------------------------------
Bug #8784: CSV - Empty fields are discarded when col_sep is a space
https://bugs.ruby-lang.org/issues/8784#change-63421

* Author: Sylvain Laperche
* Status: Assigned
* Priority: Normal
* Assignee: James Gray
* Target version: 
* ruby -v: ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-linux]
* Backport: 1.9.3: UNKNOWN, 2.0.0: UNKNOWN
----------------------------------------
When using space as column delimiter, empty fields are discarded.
With other delimiters, like comma, empty fields are correctly retrieved.

The following code reproduces the problem:
********************************************************************************
#!/usr/bin/env ruby

require 'csv'

print(CSV.parse('2009,2,3,8,0,,30.1,,'))
puts
print(CSV.parse('2009 2 3 8 0  30.1  ', col_sep: ' '))
puts
********************************************************************************

*Expected output*
[["2009", "2", "3", "8", "0", nil, "30.1", nil, nil]]
[["2009", "2", "3", "8", "0", nil, "30.1", nil, nil]]

*Current output*
[["2009", "2", "3", "8", "0", nil, "30.1", nil, nil]]
[["2009", "2", "3", "8", "0", "30.1", nil]]


---Files--------------------------------
bug_csv_col_sep.rb (142 Bytes)


-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>

In This Thread