Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/exe_root.../opt/alt/ruby30/share/ruby
File: csv.rb
# encoding: US-ASCII
[0] Fix | Delete
# frozen_string_literal: true
[1] Fix | Delete
# = csv.rb -- CSV Reading and Writing
[2] Fix | Delete
#
[3] Fix | Delete
# Created by James Edward Gray II on 2005-10-31.
[4] Fix | Delete
#
[5] Fix | Delete
# See CSV for documentation.
[6] Fix | Delete
#
[7] Fix | Delete
# == Description
[8] Fix | Delete
#
[9] Fix | Delete
# Welcome to the new and improved CSV.
[10] Fix | Delete
#
[11] Fix | Delete
# This version of the CSV library began its life as FasterCSV. FasterCSV was
[12] Fix | Delete
# intended as a replacement to Ruby's then standard CSV library. It was
[13] Fix | Delete
# designed to address concerns users of that library had and it had three
[14] Fix | Delete
# primary goals:
[15] Fix | Delete
#
[16] Fix | Delete
# 1. Be significantly faster than CSV while remaining a pure Ruby library.
[17] Fix | Delete
# 2. Use a smaller and easier to maintain code base. (FasterCSV eventually
[18] Fix | Delete
# grew larger, was also but considerably richer in features. The parsing
[19] Fix | Delete
# core remains quite small.)
[20] Fix | Delete
# 3. Improve on the CSV interface.
[21] Fix | Delete
#
[22] Fix | Delete
# Obviously, the last one is subjective. I did try to defer to the original
[23] Fix | Delete
# interface whenever I didn't have a compelling reason to change it though, so
[24] Fix | Delete
# hopefully this won't be too radically different.
[25] Fix | Delete
#
[26] Fix | Delete
# We must have met our goals because FasterCSV was renamed to CSV and replaced
[27] Fix | Delete
# the original library as of Ruby 1.9. If you are migrating code from 1.8 or
[28] Fix | Delete
# earlier, you may have to change your code to comply with the new interface.
[29] Fix | Delete
#
[30] Fix | Delete
# == What's the Different From the Old CSV?
[31] Fix | Delete
#
[32] Fix | Delete
# I'm sure I'll miss something, but I'll try to mention most of the major
[33] Fix | Delete
# differences I am aware of, to help others quickly get up to speed:
[34] Fix | Delete
#
[35] Fix | Delete
# === \CSV Parsing
[36] Fix | Delete
#
[37] Fix | Delete
# * This parser is m17n aware. See CSV for full details.
[38] Fix | Delete
# * This library has a stricter parser and will throw MalformedCSVErrors on
[39] Fix | Delete
# problematic data.
[40] Fix | Delete
# * This library has a less liberal idea of a line ending than CSV. What you
[41] Fix | Delete
# set as the <tt>:row_sep</tt> is law. It can auto-detect your line endings
[42] Fix | Delete
# though.
[43] Fix | Delete
# * The old library returned empty lines as <tt>[nil]</tt>. This library calls
[44] Fix | Delete
# them <tt>[]</tt>.
[45] Fix | Delete
# * This library has a much faster parser.
[46] Fix | Delete
#
[47] Fix | Delete
# === Interface
[48] Fix | Delete
#
[49] Fix | Delete
# * CSV now uses Hash-style parameters to set options.
[50] Fix | Delete
# * CSV no longer has generate_row() or parse_row().
[51] Fix | Delete
# * The old CSV's Reader and Writer classes have been dropped.
[52] Fix | Delete
# * CSV::open() is now more like Ruby's open().
[53] Fix | Delete
# * CSV objects now support most standard IO methods.
[54] Fix | Delete
# * CSV now has a new() method used to wrap objects like String and IO for
[55] Fix | Delete
# reading and writing.
[56] Fix | Delete
# * CSV::generate() is different from the old method.
[57] Fix | Delete
# * CSV no longer supports partial reads. It works line-by-line.
[58] Fix | Delete
# * CSV no longer allows the instance methods to override the separators for
[59] Fix | Delete
# performance reasons. They must be set in the constructor.
[60] Fix | Delete
#
[61] Fix | Delete
# If you use this library and find yourself missing any functionality I have
[62] Fix | Delete
# trimmed, please {let me know}[mailto:james@grayproductions.net].
[63] Fix | Delete
#
[64] Fix | Delete
# == Documentation
[65] Fix | Delete
#
[66] Fix | Delete
# See CSV for documentation.
[67] Fix | Delete
#
[68] Fix | Delete
# == What is CSV, really?
[69] Fix | Delete
#
[70] Fix | Delete
# CSV maintains a pretty strict definition of CSV taken directly from
[71] Fix | Delete
# {the RFC}[http://www.ietf.org/rfc/rfc4180.txt]. I relax the rules in only one
[72] Fix | Delete
# place and that is to make using this library easier. CSV will parse all valid
[73] Fix | Delete
# CSV.
[74] Fix | Delete
#
[75] Fix | Delete
# What you don't want to do is to feed CSV invalid data. Because of the way the
[76] Fix | Delete
# CSV format works, it's common for a parser to need to read until the end of
[77] Fix | Delete
# the file to be sure a field is invalid. This consumes a lot of time and memory.
[78] Fix | Delete
#
[79] Fix | Delete
# Luckily, when working with invalid CSV, Ruby's built-in methods will almost
[80] Fix | Delete
# always be superior in every way. For example, parsing non-quoted fields is as
[81] Fix | Delete
# easy as:
[82] Fix | Delete
#
[83] Fix | Delete
# data.split(",")
[84] Fix | Delete
#
[85] Fix | Delete
# == Questions and/or Comments
[86] Fix | Delete
#
[87] Fix | Delete
# Feel free to email {James Edward Gray II}[mailto:james@grayproductions.net]
[88] Fix | Delete
# with any questions.
[89] Fix | Delete
[90] Fix | Delete
require "forwardable"
[91] Fix | Delete
require "English"
[92] Fix | Delete
require "date"
[93] Fix | Delete
require "stringio"
[94] Fix | Delete
[95] Fix | Delete
require_relative "csv/fields_converter"
[96] Fix | Delete
require_relative "csv/match_p"
[97] Fix | Delete
require_relative "csv/parser"
[98] Fix | Delete
require_relative "csv/row"
[99] Fix | Delete
require_relative "csv/table"
[100] Fix | Delete
require_relative "csv/writer"
[101] Fix | Delete
[102] Fix | Delete
using CSV::MatchP if CSV.const_defined?(:MatchP)
[103] Fix | Delete
[104] Fix | Delete
# == \CSV
[105] Fix | Delete
# \CSV (comma-separated variables) data is a text representation of a table:
[106] Fix | Delete
# - A _row_ _separator_ delimits table rows.
[107] Fix | Delete
# A common row separator is the newline character <tt>"\n"</tt>.
[108] Fix | Delete
# - A _column_ _separator_ delimits fields in a row.
[109] Fix | Delete
# A common column separator is the comma character <tt>","</tt>.
[110] Fix | Delete
#
[111] Fix | Delete
# This \CSV \String, with row separator <tt>"\n"</tt>
[112] Fix | Delete
# and column separator <tt>","</tt>,
[113] Fix | Delete
# has three rows and two columns:
[114] Fix | Delete
# "foo,0\nbar,1\nbaz,2\n"
[115] Fix | Delete
#
[116] Fix | Delete
# Despite the name \CSV, a \CSV representation can use different separators.
[117] Fix | Delete
#
[118] Fix | Delete
# For more about tables, see the Wikipedia article
[119] Fix | Delete
# "{Table (information)}[https://en.wikipedia.org/wiki/Table_(information)]",
[120] Fix | Delete
# especially its section
[121] Fix | Delete
# "{Simple table}[https://en.wikipedia.org/wiki/Table_(information)#Simple_table]"
[122] Fix | Delete
#
[123] Fix | Delete
# == \Class \CSV
[124] Fix | Delete
#
[125] Fix | Delete
# Class \CSV provides methods for:
[126] Fix | Delete
# - Parsing \CSV data from a \String object, a \File (via its file path), or an \IO object.
[127] Fix | Delete
# - Generating \CSV data to a \String object.
[128] Fix | Delete
#
[129] Fix | Delete
# To make \CSV available:
[130] Fix | Delete
# require 'csv'
[131] Fix | Delete
#
[132] Fix | Delete
# All examples here assume that this has been done.
[133] Fix | Delete
#
[134] Fix | Delete
# == Keeping It Simple
[135] Fix | Delete
#
[136] Fix | Delete
# A \CSV object has dozens of instance methods that offer fine-grained control
[137] Fix | Delete
# of parsing and generating \CSV data.
[138] Fix | Delete
# For many needs, though, simpler approaches will do.
[139] Fix | Delete
#
[140] Fix | Delete
# This section summarizes the singleton methods in \CSV
[141] Fix | Delete
# that allow you to parse and generate without explicitly
[142] Fix | Delete
# creating \CSV objects.
[143] Fix | Delete
# For details, follow the links.
[144] Fix | Delete
#
[145] Fix | Delete
# === Simple Parsing
[146] Fix | Delete
#
[147] Fix | Delete
# Parsing methods commonly return either of:
[148] Fix | Delete
# - An \Array of Arrays of Strings:
[149] Fix | Delete
# - The outer \Array is the entire "table".
[150] Fix | Delete
# - Each inner \Array is a row.
[151] Fix | Delete
# - Each \String is a field.
[152] Fix | Delete
# - A CSV::Table object. For details, see
[153] Fix | Delete
# {\CSV with Headers}[#class-CSV-label-CSV+with+Headers].
[154] Fix | Delete
#
[155] Fix | Delete
# ==== Parsing a \String
[156] Fix | Delete
#
[157] Fix | Delete
# The input to be parsed can be a string:
[158] Fix | Delete
# string = "foo,0\nbar,1\nbaz,2\n"
[159] Fix | Delete
#
[160] Fix | Delete
# \Method CSV.parse returns the entire \CSV data:
[161] Fix | Delete
# CSV.parse(string) # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
[162] Fix | Delete
#
[163] Fix | Delete
# \Method CSV.parse_line returns only the first row:
[164] Fix | Delete
# CSV.parse_line(string) # => ["foo", "0"]
[165] Fix | Delete
#
[166] Fix | Delete
# \CSV extends class \String with instance method String#parse_csv,
[167] Fix | Delete
# which also returns only the first row:
[168] Fix | Delete
# string.parse_csv # => ["foo", "0"]
[169] Fix | Delete
#
[170] Fix | Delete
# ==== Parsing Via a \File Path
[171] Fix | Delete
#
[172] Fix | Delete
# The input to be parsed can be in a file:
[173] Fix | Delete
# string = "foo,0\nbar,1\nbaz,2\n"
[174] Fix | Delete
# path = 't.csv'
[175] Fix | Delete
# File.write(path, string)
[176] Fix | Delete
#
[177] Fix | Delete
# \Method CSV.read returns the entire \CSV data:
[178] Fix | Delete
# CSV.read(path) # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
[179] Fix | Delete
#
[180] Fix | Delete
# \Method CSV.foreach iterates, passing each row to the given block:
[181] Fix | Delete
# CSV.foreach(path) do |row|
[182] Fix | Delete
# p row
[183] Fix | Delete
# end
[184] Fix | Delete
# Output:
[185] Fix | Delete
# ["foo", "0"]
[186] Fix | Delete
# ["bar", "1"]
[187] Fix | Delete
# ["baz", "2"]
[188] Fix | Delete
#
[189] Fix | Delete
# \Method CSV.table returns the entire \CSV data as a CSV::Table object:
[190] Fix | Delete
# CSV.table(path) # => #<CSV::Table mode:col_or_row row_count:3>
[191] Fix | Delete
#
[192] Fix | Delete
# ==== Parsing from an Open \IO Stream
[193] Fix | Delete
#
[194] Fix | Delete
# The input to be parsed can be in an open \IO stream:
[195] Fix | Delete
#
[196] Fix | Delete
# \Method CSV.read returns the entire \CSV data:
[197] Fix | Delete
# File.open(path) do |file|
[198] Fix | Delete
# CSV.read(file)
[199] Fix | Delete
# end # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
[200] Fix | Delete
#
[201] Fix | Delete
# As does method CSV.parse:
[202] Fix | Delete
# File.open(path) do |file|
[203] Fix | Delete
# CSV.parse(file)
[204] Fix | Delete
# end # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
[205] Fix | Delete
#
[206] Fix | Delete
# \Method CSV.parse_line returns only the first row:
[207] Fix | Delete
# File.open(path) do |file|
[208] Fix | Delete
# CSV.parse_line(file)
[209] Fix | Delete
# end # => ["foo", "0"]
[210] Fix | Delete
#
[211] Fix | Delete
# \Method CSV.foreach iterates, passing each row to the given block:
[212] Fix | Delete
# File.open(path) do |file|
[213] Fix | Delete
# CSV.foreach(file) do |row|
[214] Fix | Delete
# p row
[215] Fix | Delete
# end
[216] Fix | Delete
# end
[217] Fix | Delete
# Output:
[218] Fix | Delete
# ["foo", "0"]
[219] Fix | Delete
# ["bar", "1"]
[220] Fix | Delete
# ["baz", "2"]
[221] Fix | Delete
#
[222] Fix | Delete
# \Method CSV.table returns the entire \CSV data as a CSV::Table object:
[223] Fix | Delete
# File.open(path) do |file|
[224] Fix | Delete
# CSV.table(file)
[225] Fix | Delete
# end # => #<CSV::Table mode:col_or_row row_count:3>
[226] Fix | Delete
#
[227] Fix | Delete
# === Simple Generating
[228] Fix | Delete
#
[229] Fix | Delete
# \Method CSV.generate returns a \String;
[230] Fix | Delete
# this example uses method CSV#<< to append the rows
[231] Fix | Delete
# that are to be generated:
[232] Fix | Delete
# output_string = CSV.generate do |csv|
[233] Fix | Delete
# csv << ['foo', 0]
[234] Fix | Delete
# csv << ['bar', 1]
[235] Fix | Delete
# csv << ['baz', 2]
[236] Fix | Delete
# end
[237] Fix | Delete
# output_string # => "foo,0\nbar,1\nbaz,2\n"
[238] Fix | Delete
#
[239] Fix | Delete
# \Method CSV.generate_line returns a \String containing the single row
[240] Fix | Delete
# constructed from an \Array:
[241] Fix | Delete
# CSV.generate_line(['foo', '0']) # => "foo,0\n"
[242] Fix | Delete
#
[243] Fix | Delete
# \CSV extends class \Array with instance method <tt>Array#to_csv</tt>,
[244] Fix | Delete
# which forms an \Array into a \String:
[245] Fix | Delete
# ['foo', '0'].to_csv # => "foo,0\n"
[246] Fix | Delete
#
[247] Fix | Delete
# === "Filtering" \CSV
[248] Fix | Delete
#
[249] Fix | Delete
# \Method CSV.filter provides a Unix-style filter for \CSV data.
[250] Fix | Delete
# The input data is processed to form the output data:
[251] Fix | Delete
# in_string = "foo,0\nbar,1\nbaz,2\n"
[252] Fix | Delete
# out_string = ''
[253] Fix | Delete
# CSV.filter(in_string, out_string) do |row|
[254] Fix | Delete
# row[0] = row[0].upcase
[255] Fix | Delete
# row[1] *= 4
[256] Fix | Delete
# end
[257] Fix | Delete
# out_string # => "FOO,0000\nBAR,1111\nBAZ,2222\n"
[258] Fix | Delete
#
[259] Fix | Delete
# == \CSV Objects
[260] Fix | Delete
#
[261] Fix | Delete
# There are three ways to create a \CSV object:
[262] Fix | Delete
# - \Method CSV.new returns a new \CSV object.
[263] Fix | Delete
# - \Method CSV.instance returns a new or cached \CSV object.
[264] Fix | Delete
# - \Method \CSV() also returns a new or cached \CSV object.
[265] Fix | Delete
#
[266] Fix | Delete
# === Instance Methods
[267] Fix | Delete
#
[268] Fix | Delete
# \CSV has three groups of instance methods:
[269] Fix | Delete
# - Its own internally defined instance methods.
[270] Fix | Delete
# - Methods included by module Enumerable.
[271] Fix | Delete
# - Methods delegated to class IO. See below.
[272] Fix | Delete
#
[273] Fix | Delete
# ==== Delegated Methods
[274] Fix | Delete
#
[275] Fix | Delete
# For convenience, a CSV object will delegate to many methods in class IO.
[276] Fix | Delete
# (A few have wrapper "guard code" in \CSV.) You may call:
[277] Fix | Delete
# * IO#binmode
[278] Fix | Delete
# * #binmode?
[279] Fix | Delete
# * IO#close
[280] Fix | Delete
# * IO#close_read
[281] Fix | Delete
# * IO#close_write
[282] Fix | Delete
# * IO#closed?
[283] Fix | Delete
# * #eof
[284] Fix | Delete
# * #eof?
[285] Fix | Delete
# * IO#external_encoding
[286] Fix | Delete
# * IO#fcntl
[287] Fix | Delete
# * IO#fileno
[288] Fix | Delete
# * #flock
[289] Fix | Delete
# * IO#flush
[290] Fix | Delete
# * IO#fsync
[291] Fix | Delete
# * IO#internal_encoding
[292] Fix | Delete
# * #ioctl
[293] Fix | Delete
# * IO#isatty
[294] Fix | Delete
# * #path
[295] Fix | Delete
# * IO#pid
[296] Fix | Delete
# * IO#pos
[297] Fix | Delete
# * IO#pos=
[298] Fix | Delete
# * IO#reopen
[299] Fix | Delete
# * #rewind
[300] Fix | Delete
# * IO#seek
[301] Fix | Delete
# * #stat
[302] Fix | Delete
# * IO#string
[303] Fix | Delete
# * IO#sync
[304] Fix | Delete
# * IO#sync=
[305] Fix | Delete
# * IO#tell
[306] Fix | Delete
# * #to_i
[307] Fix | Delete
# * #to_io
[308] Fix | Delete
# * IO#truncate
[309] Fix | Delete
# * IO#tty?
[310] Fix | Delete
#
[311] Fix | Delete
# === Options
[312] Fix | Delete
#
[313] Fix | Delete
# The default values for options are:
[314] Fix | Delete
# DEFAULT_OPTIONS = {
[315] Fix | Delete
# # For both parsing and generating.
[316] Fix | Delete
# col_sep: ",",
[317] Fix | Delete
# row_sep: :auto,
[318] Fix | Delete
# quote_char: '"',
[319] Fix | Delete
# # For parsing.
[320] Fix | Delete
# field_size_limit: nil,
[321] Fix | Delete
# converters: nil,
[322] Fix | Delete
# unconverted_fields: nil,
[323] Fix | Delete
# headers: false,
[324] Fix | Delete
# return_headers: false,
[325] Fix | Delete
# header_converters: nil,
[326] Fix | Delete
# skip_blanks: false,
[327] Fix | Delete
# skip_lines: nil,
[328] Fix | Delete
# liberal_parsing: false,
[329] Fix | Delete
# nil_value: nil,
[330] Fix | Delete
# empty_value: "",
[331] Fix | Delete
# # For generating.
[332] Fix | Delete
# write_headers: nil,
[333] Fix | Delete
# quote_empty: true,
[334] Fix | Delete
# force_quotes: false,
[335] Fix | Delete
# write_converters: nil,
[336] Fix | Delete
# write_nil_value: nil,
[337] Fix | Delete
# write_empty_value: "",
[338] Fix | Delete
# strip: false,
[339] Fix | Delete
# }
[340] Fix | Delete
#
[341] Fix | Delete
# ==== Options for Parsing
[342] Fix | Delete
#
[343] Fix | Delete
# Options for parsing, described in detail below, include:
[344] Fix | Delete
# - +row_sep+: Specifies the row separator; used to delimit rows.
[345] Fix | Delete
# - +col_sep+: Specifies the column separator; used to delimit fields.
[346] Fix | Delete
# - +quote_char+: Specifies the quote character; used to quote fields.
[347] Fix | Delete
# - +field_size_limit+: Specifies the maximum field size allowed.
[348] Fix | Delete
# - +converters+: Specifies the field converters to be used.
[349] Fix | Delete
# - +unconverted_fields+: Specifies whether unconverted fields are to be available.
[350] Fix | Delete
# - +headers+: Specifies whether data contains headers,
[351] Fix | Delete
# or specifies the headers themselves.
[352] Fix | Delete
# - +return_headers+: Specifies whether headers are to be returned.
[353] Fix | Delete
# - +header_converters+: Specifies the header converters to be used.
[354] Fix | Delete
# - +skip_blanks+: Specifies whether blanks lines are to be ignored.
[355] Fix | Delete
# - +skip_lines+: Specifies how comments lines are to be recognized.
[356] Fix | Delete
# - +strip+: Specifies whether leading and trailing whitespace are
[357] Fix | Delete
# to be stripped from fields..
[358] Fix | Delete
# - +liberal_parsing+: Specifies whether \CSV should attempt to parse
[359] Fix | Delete
# non-compliant data.
[360] Fix | Delete
# - +nil_value+: Specifies the object that is to be substituted for each null (no-text) field.
[361] Fix | Delete
# - +empty_value+: Specifies the object that is to be substituted for each empty field.
[362] Fix | Delete
#
[363] Fix | Delete
# :include: ../doc/csv/options/common/row_sep.rdoc
[364] Fix | Delete
#
[365] Fix | Delete
# :include: ../doc/csv/options/common/col_sep.rdoc
[366] Fix | Delete
#
[367] Fix | Delete
# :include: ../doc/csv/options/common/quote_char.rdoc
[368] Fix | Delete
#
[369] Fix | Delete
# :include: ../doc/csv/options/parsing/field_size_limit.rdoc
[370] Fix | Delete
#
[371] Fix | Delete
# :include: ../doc/csv/options/parsing/converters.rdoc
[372] Fix | Delete
#
[373] Fix | Delete
# :include: ../doc/csv/options/parsing/unconverted_fields.rdoc
[374] Fix | Delete
#
[375] Fix | Delete
# :include: ../doc/csv/options/parsing/headers.rdoc
[376] Fix | Delete
#
[377] Fix | Delete
# :include: ../doc/csv/options/parsing/return_headers.rdoc
[378] Fix | Delete
#
[379] Fix | Delete
# :include: ../doc/csv/options/parsing/header_converters.rdoc
[380] Fix | Delete
#
[381] Fix | Delete
# :include: ../doc/csv/options/parsing/skip_blanks.rdoc
[382] Fix | Delete
#
[383] Fix | Delete
# :include: ../doc/csv/options/parsing/skip_lines.rdoc
[384] Fix | Delete
#
[385] Fix | Delete
# :include: ../doc/csv/options/parsing/strip.rdoc
[386] Fix | Delete
#
[387] Fix | Delete
# :include: ../doc/csv/options/parsing/liberal_parsing.rdoc
[388] Fix | Delete
#
[389] Fix | Delete
# :include: ../doc/csv/options/parsing/nil_value.rdoc
[390] Fix | Delete
#
[391] Fix | Delete
# :include: ../doc/csv/options/parsing/empty_value.rdoc
[392] Fix | Delete
#
[393] Fix | Delete
# ==== Options for Generating
[394] Fix | Delete
#
[395] Fix | Delete
# Options for generating, described in detail below, include:
[396] Fix | Delete
# - +row_sep+: Specifies the row separator; used to delimit rows.
[397] Fix | Delete
# - +col_sep+: Specifies the column separator; used to delimit fields.
[398] Fix | Delete
# - +quote_char+: Specifies the quote character; used to quote fields.
[399] Fix | Delete
# - +write_headers+: Specifies whether headers are to be written.
[400] Fix | Delete
# - +force_quotes+: Specifies whether each output field is to be quoted.
[401] Fix | Delete
# - +quote_empty+: Specifies whether each empty output field is to be quoted.
[402] Fix | Delete
# - +write_converters+: Specifies the field converters to be used in writing.
[403] Fix | Delete
# - +write_nil_value+: Specifies the object that is to be substituted for each +nil+-valued field.
[404] Fix | Delete
# - +write_empty_value+: Specifies the object that is to be substituted for each empty field.
[405] Fix | Delete
#
[406] Fix | Delete
# :include: ../doc/csv/options/common/row_sep.rdoc
[407] Fix | Delete
#
[408] Fix | Delete
# :include: ../doc/csv/options/common/col_sep.rdoc
[409] Fix | Delete
#
[410] Fix | Delete
# :include: ../doc/csv/options/common/quote_char.rdoc
[411] Fix | Delete
#
[412] Fix | Delete
# :include: ../doc/csv/options/generating/write_headers.rdoc
[413] Fix | Delete
#
[414] Fix | Delete
# :include: ../doc/csv/options/generating/force_quotes.rdoc
[415] Fix | Delete
#
[416] Fix | Delete
# :include: ../doc/csv/options/generating/quote_empty.rdoc
[417] Fix | Delete
#
[418] Fix | Delete
# :include: ../doc/csv/options/generating/write_converters.rdoc
[419] Fix | Delete
#
[420] Fix | Delete
# :include: ../doc/csv/options/generating/write_nil_value.rdoc
[421] Fix | Delete
#
[422] Fix | Delete
# :include: ../doc/csv/options/generating/write_empty_value.rdoc
[423] Fix | Delete
#
[424] Fix | Delete
# === \CSV with Headers
[425] Fix | Delete
#
[426] Fix | Delete
# CSV allows to specify column names of CSV file, whether they are in data, or
[427] Fix | Delete
# provided separately. If headers are specified, reading methods return an instance
[428] Fix | Delete
# of CSV::Table, consisting of CSV::Row.
[429] Fix | Delete
#
[430] Fix | Delete
# # Headers are part of data
[431] Fix | Delete
# data = CSV.parse(<<~ROWS, headers: true)
[432] Fix | Delete
# Name,Department,Salary
[433] Fix | Delete
# Bob,Engineering,1000
[434] Fix | Delete
# Jane,Sales,2000
[435] Fix | Delete
# John,Management,5000
[436] Fix | Delete
# ROWS
[437] Fix | Delete
#
[438] Fix | Delete
# data.class #=> CSV::Table
[439] Fix | Delete
# data.first #=> #<CSV::Row "Name":"Bob" "Department":"Engineering" "Salary":"1000">
[440] Fix | Delete
# data.first.to_h #=> {"Name"=>"Bob", "Department"=>"Engineering", "Salary"=>"1000"}
[441] Fix | Delete
#
[442] Fix | Delete
# # Headers provided by developer
[443] Fix | Delete
# data = CSV.parse('Bob,Engineering,1000', headers: %i[name department salary])
[444] Fix | Delete
# data.first #=> #<CSV::Row name:"Bob" department:"Engineering" salary:"1000">
[445] Fix | Delete
#
[446] Fix | Delete
# === \Converters
[447] Fix | Delete
#
[448] Fix | Delete
# By default, each value (field or header) parsed by \CSV is formed into a \String.
[449] Fix | Delete
# You can use a _field_ _converter_ or _header_ _converter_
[450] Fix | Delete
# to intercept and modify the parsed values:
[451] Fix | Delete
# - See {Field Converters}[#class-CSV-label-Field+Converters].
[452] Fix | Delete
# - See {Header Converters}[#class-CSV-label-Header+Converters].
[453] Fix | Delete
#
[454] Fix | Delete
# Also by default, each value to be written during generation is written 'as-is'.
[455] Fix | Delete
# You can use a _write_ _converter_ to modify values before writing.
[456] Fix | Delete
# - See {Write Converters}[#class-CSV-label-Write+Converters].
[457] Fix | Delete
#
[458] Fix | Delete
# ==== Specifying \Converters
[459] Fix | Delete
#
[460] Fix | Delete
# You can specify converters for parsing or generating in the +options+
[461] Fix | Delete
# argument to various \CSV methods:
[462] Fix | Delete
# - Option +converters+ for converting parsed field values.
[463] Fix | Delete
# - Option +header_converters+ for converting parsed header values.
[464] Fix | Delete
# - Option +write_converters+ for converting values to be written (generated).
[465] Fix | Delete
#
[466] Fix | Delete
# There are three forms for specifying converters:
[467] Fix | Delete
# - A converter proc: executable code to be used for conversion.
[468] Fix | Delete
# - A converter name: the name of a stored converter.
[469] Fix | Delete
# - A converter list: an array of converter procs, converter names, and converter lists.
[470] Fix | Delete
#
[471] Fix | Delete
# ===== Converter Procs
[472] Fix | Delete
#
[473] Fix | Delete
# This converter proc, +strip_converter+, accepts a value +field+
[474] Fix | Delete
# and returns <tt>field.strip</tt>:
[475] Fix | Delete
# strip_converter = proc {|field| field.strip }
[476] Fix | Delete
# In this call to <tt>CSV.parse</tt>,
[477] Fix | Delete
# the keyword argument <tt>converters: string_converter</tt>
[478] Fix | Delete
# specifies that:
[479] Fix | Delete
# - \Proc +string_converter+ is to be called for each parsed field.
[480] Fix | Delete
# - The converter's return value is to replace the +field+ value.
[481] Fix | Delete
# Example:
[482] Fix | Delete
# string = " foo , 0 \n bar , 1 \n baz , 2 \n"
[483] Fix | Delete
# array = CSV.parse(string, converters: strip_converter)
[484] Fix | Delete
# array # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
[485] Fix | Delete
#
[486] Fix | Delete
# A converter proc can receive a second argument, +field_info+,
[487] Fix | Delete
# that contains details about the field.
[488] Fix | Delete
# This modified +strip_converter+ displays its arguments:
[489] Fix | Delete
# strip_converter = proc do |field, field_info|
[490] Fix | Delete
# p [field, field_info]
[491] Fix | Delete
# field.strip
[492] Fix | Delete
# end
[493] Fix | Delete
# string = " foo , 0 \n bar , 1 \n baz , 2 \n"
[494] Fix | Delete
# array = CSV.parse(string, converters: strip_converter)
[495] Fix | Delete
# array # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
[496] Fix | Delete
# Output:
[497] Fix | Delete
# [" foo ", #<struct CSV::FieldInfo index=0, line=1, header=nil>]
[498] Fix | Delete
# [" 0 ", #<struct CSV::FieldInfo index=1, line=1, header=nil>]
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function