This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
====== Option +col_sep+
|
||||
|
||||
Specifies the \String column separator to be used
|
||||
for both parsing and generating.
|
||||
The \String will be transcoded into the data's \Encoding before use.
|
||||
|
||||
Default value:
|
||||
CSV::DEFAULT_OPTIONS.fetch(:col_sep) # => "," (comma)
|
||||
|
||||
Using the default (comma):
|
||||
str = CSV.generate do |csv|
|
||||
csv << [:foo, 0]
|
||||
csv << [:bar, 1]
|
||||
csv << [:baz, 2]
|
||||
end
|
||||
str # => "foo,0\nbar,1\nbaz,2\n"
|
||||
ary = CSV.parse(str)
|
||||
ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
||||
|
||||
Using +:+ (colon):
|
||||
col_sep = ':'
|
||||
str = CSV.generate(col_sep: col_sep) do |csv|
|
||||
csv << [:foo, 0]
|
||||
csv << [:bar, 1]
|
||||
csv << [:baz, 2]
|
||||
end
|
||||
str # => "foo:0\nbar:1\nbaz:2\n"
|
||||
ary = CSV.parse(str, col_sep: col_sep)
|
||||
ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
||||
|
||||
Using +::+ (two colons):
|
||||
col_sep = '::'
|
||||
str = CSV.generate(col_sep: col_sep) do |csv|
|
||||
csv << [:foo, 0]
|
||||
csv << [:bar, 1]
|
||||
csv << [:baz, 2]
|
||||
end
|
||||
str # => "foo::0\nbar::1\nbaz::2\n"
|
||||
ary = CSV.parse(str, col_sep: col_sep)
|
||||
ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
||||
|
||||
Using <tt>''</tt> (empty string):
|
||||
col_sep = ''
|
||||
str = CSV.generate(col_sep: col_sep) do |csv|
|
||||
csv << [:foo, 0]
|
||||
csv << [:bar, 1]
|
||||
csv << [:baz, 2]
|
||||
end
|
||||
str # => "foo0\nbar1\nbaz2\n"
|
||||
|
||||
---
|
||||
|
||||
Raises an exception if parsing with the empty \String:
|
||||
col_sep = ''
|
||||
# Raises ArgumentError (:col_sep must be 1 or more characters: "")
|
||||
CSV.parse("foo0\nbar1\nbaz2\n", col_sep: col_sep)
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
====== Option +quote_char+
|
||||
|
||||
Specifies the character (\String of length 1) used used to quote fields
|
||||
in both parsing and generating.
|
||||
This String will be transcoded into the data's \Encoding before use.
|
||||
|
||||
Default value:
|
||||
CSV::DEFAULT_OPTIONS.fetch(:quote_char) # => "\"" (double quote)
|
||||
|
||||
This is useful for an application that incorrectly uses <tt>'</tt> (single-quote)
|
||||
to quote fields, instead of the correct <tt>"</tt> (double-quote).
|
||||
|
||||
Using the default (double quote):
|
||||
str = CSV.generate do |csv|
|
||||
csv << ['foo', 0]
|
||||
csv << ["'bar'", 1]
|
||||
csv << ['"baz"', 2]
|
||||
end
|
||||
str # => "foo,0\n'bar',1\n\"\"\"baz\"\"\",2\n"
|
||||
ary = CSV.parse(str)
|
||||
ary # => [["foo", "0"], ["'bar'", "1"], ["\"baz\"", "2"]]
|
||||
|
||||
Using <tt>'</tt> (single-quote):
|
||||
quote_char = "'"
|
||||
str = CSV.generate(quote_char: quote_char) do |csv|
|
||||
csv << ['foo', 0]
|
||||
csv << ["'bar'", 1]
|
||||
csv << ['"baz"', 2]
|
||||
end
|
||||
str # => "foo,0\n'''bar''',1\n\"baz\",2\n"
|
||||
ary = CSV.parse(str, quote_char: quote_char)
|
||||
ary # => [["foo", "0"], ["'bar'", "1"], ["\"baz\"", "2"]]
|
||||
|
||||
---
|
||||
|
||||
Raises an exception if the \String length is greater than 1:
|
||||
# Raises ArgumentError (:quote_char has to be nil or a single character String)
|
||||
CSV.new('', quote_char: 'xx')
|
||||
|
||||
Raises an exception if the value is not a \String:
|
||||
# Raises ArgumentError (:quote_char has to be nil or a single character String)
|
||||
CSV.new('', quote_char: :foo)
|
||||
@@ -0,0 +1,91 @@
|
||||
====== Option +row_sep+
|
||||
|
||||
Specifies the row separator, a \String or the \Symbol <tt>:auto</tt> (see below),
|
||||
to be used for both parsing and generating.
|
||||
|
||||
Default value:
|
||||
CSV::DEFAULT_OPTIONS.fetch(:row_sep) # => :auto
|
||||
|
||||
---
|
||||
|
||||
When +row_sep+ is a \String, that \String becomes the row separator.
|
||||
The String will be transcoded into the data's Encoding before use.
|
||||
|
||||
Using <tt>"\n"</tt>:
|
||||
row_sep = "\n"
|
||||
str = CSV.generate(row_sep: row_sep) do |csv|
|
||||
csv << [:foo, 0]
|
||||
csv << [:bar, 1]
|
||||
csv << [:baz, 2]
|
||||
end
|
||||
str # => "foo,0\nbar,1\nbaz,2\n"
|
||||
ary = CSV.parse(str)
|
||||
ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
||||
|
||||
Using <tt>|</tt> (pipe):
|
||||
row_sep = '|'
|
||||
str = CSV.generate(row_sep: row_sep) do |csv|
|
||||
csv << [:foo, 0]
|
||||
csv << [:bar, 1]
|
||||
csv << [:baz, 2]
|
||||
end
|
||||
str # => "foo,0|bar,1|baz,2|"
|
||||
ary = CSV.parse(str, row_sep: row_sep)
|
||||
ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
||||
|
||||
Using <tt>--</tt> (two hyphens):
|
||||
row_sep = '--'
|
||||
str = CSV.generate(row_sep: row_sep) do |csv|
|
||||
csv << [:foo, 0]
|
||||
csv << [:bar, 1]
|
||||
csv << [:baz, 2]
|
||||
end
|
||||
str # => "foo,0--bar,1--baz,2--"
|
||||
ary = CSV.parse(str, row_sep: row_sep)
|
||||
ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
||||
|
||||
Using <tt>''</tt> (empty string):
|
||||
row_sep = ''
|
||||
str = CSV.generate(row_sep: row_sep) do |csv|
|
||||
csv << [:foo, 0]
|
||||
csv << [:bar, 1]
|
||||
csv << [:baz, 2]
|
||||
end
|
||||
str # => "foo,0bar,1baz,2"
|
||||
ary = CSV.parse(str, row_sep: row_sep)
|
||||
ary # => [["foo", "0bar", "1baz", "2"]]
|
||||
|
||||
---
|
||||
|
||||
When +row_sep+ is the \Symbol +:auto+ (the default),
|
||||
generating uses <tt>"\n"</tt> as the row separator:
|
||||
str = CSV.generate do |csv|
|
||||
csv << [:foo, 0]
|
||||
csv << [:bar, 1]
|
||||
csv << [:baz, 2]
|
||||
end
|
||||
str # => "foo,0\nbar,1\nbaz,2\n"
|
||||
|
||||
Parsing, on the other hand, invokes auto-discovery of the row separator.
|
||||
|
||||
Auto-discovery reads ahead in the data looking for the next <tt>\r\n</tt>, +\n+, or +\r+ sequence.
|
||||
The sequence will be selected even if it occurs in a quoted field,
|
||||
assuming that you would have the same line endings there.
|
||||
|
||||
Example:
|
||||
str = CSV.generate do |csv|
|
||||
csv << [:foo, 0]
|
||||
csv << [:bar, 1]
|
||||
csv << [:baz, 2]
|
||||
end
|
||||
str # => "foo,0\nbar,1\nbaz,2\n"
|
||||
ary = CSV.parse(str)
|
||||
ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
||||
|
||||
The default <tt>$INPUT_RECORD_SEPARATOR</tt> (<tt>$/</tt>) is used
|
||||
if any of the following is true:
|
||||
* None of those sequences is found.
|
||||
* Data is +ARGF+, +STDIN+, +STDOUT+, or +STDERR+.
|
||||
* The stream is only available for output.
|
||||
|
||||
Obviously, discovery takes a little time. Set manually if speed is important. Also note that IO objects should be opened in binary mode on Windows if this feature will be used as the line-ending translation can cause problems with resetting the document position to where it was before the read ahead.
|
||||
Reference in New Issue
Block a user