|
103 | 103 |
|
104 | 104 | using CSV::MatchP if CSV.const_defined?(:MatchP)
|
105 | 105 |
|
106 |
| -# This class provides a complete interface to CSV files and data. It offers |
107 |
| -# tools to enable you to read and write to and from Strings or IO objects, as |
108 |
| -# needed. |
| 106 | +# == \CSV |
| 107 | +# \CSV (comma-separated variables) data is a text representation of a table: |
| 108 | +# - A _row_ _separator_ delimits table rows. |
| 109 | +# A common row separator is the newline character <tt>"\n"</tt>. |
| 110 | +# - A _column_ _separator_ delimits fields in a row. |
| 111 | +# A common column separator is the comma character <tt>","</tt>. |
109 | 112 | #
|
110 |
| -# All examples here assume prior execution of: |
| 113 | +# This \CSV \String, with row separator <tt>"\n"</tt> |
| 114 | +# and column separator <tt>","</tt>, |
| 115 | +# has three rows and two columns: |
| 116 | +# "foo,0\nbar,1\nbaz,2\n" |
| 117 | +# |
| 118 | +# Despite the name \CSV, a \CSV representation can use different separators. |
| 119 | +# |
| 120 | +# == \Class \CSV |
| 121 | +# |
| 122 | +# Class \CSV provides methods for: |
| 123 | +# - Parsing \CSV data from a \String object, a \File (via its file path), or an \IO object. |
| 124 | +# - Generating \CSV data to a \String object. |
| 125 | +# |
| 126 | +# To make \CSV available: |
111 | 127 | # require 'csv'
|
112 | 128 | #
|
113 |
| -# The most generic interface of the library is: |
| 129 | +# All examples here assume that this has been done. |
114 | 130 | #
|
115 |
| -# csv = CSV.new(io, **options) |
| 131 | +# == Keeping It Simple |
116 | 132 | #
|
117 |
| -# # Reading: IO object should be open for read |
118 |
| -# csv.read # => array of rows |
119 |
| -# # or |
120 |
| -# csv.each do |row| |
121 |
| -# # ... |
122 |
| -# end |
123 |
| -# # or |
124 |
| -# row = csv.shift |
| 133 | +# A \CSV object has dozens of instance methods that offer fine-grained control |
| 134 | +# of parsing and generating \CSV data. |
| 135 | +# For many needs, though, simpler approaches will do. |
125 | 136 | #
|
126 |
| -# # Writing: IO object should be open for write |
127 |
| -# csv << row |
| 137 | +# This section summarizes the singleton methods in \CSV |
| 138 | +# that allow you to parse and generate without explicitly |
| 139 | +# creating \CSV objects. |
| 140 | +# For details, follow the links. |
128 | 141 | #
|
129 |
| -# There are several specialized class methods for one-statement reading or writing, |
130 |
| -# described in the Specialized Methods section. |
| 142 | +# === Simple Parsing |
131 | 143 | #
|
132 |
| -# If a String is passed into ::new, it is internally wrapped into a StringIO object. |
| 144 | +# Parsing methods commonly return either of: |
| 145 | +# - An \Array of Arrays of Strings: |
| 146 | +# - The outer \Array is the entire "table". |
| 147 | +# - Each inner \Array is a row. |
| 148 | +# - Each \String is a field. |
| 149 | +# - A CSV::Table object. For details, see |
| 150 | +# {\CSV with Headers}[#class-CSV-label-CSV+with+Headers]. |
133 | 151 | #
|
134 |
| -# +options+ can be used for specifying the particular CSV flavor (column |
135 |
| -# separators, row separators, value quoting and so on), and for data conversion, |
136 |
| -# see Data Conversion section for the description of the latter. |
| 152 | +# ==== Parsing a \String |
137 | 153 | #
|
138 |
| -# == Specialized Methods |
| 154 | +# The input to be parsed can be a string: |
| 155 | +# string = "foo,0\nbar,1\nbaz,2\n" |
139 | 156 | #
|
140 |
| -# === Reading |
| 157 | +# \Method CSV.parse returns the entire \CSV data: |
| 158 | +# CSV.parse(string) # => [["foo", "0"], ["bar", "1"], ["baz", "2"]] |
141 | 159 | #
|
142 |
| -# # From a file: all at once |
143 |
| -# arr_of_rows = CSV.read("path/to/file.csv", **options) |
144 |
| -# # iterator-style: |
145 |
| -# CSV.foreach("path/to/file.csv", **options) do |row| |
146 |
| -# # ... |
147 |
| -# end |
| 160 | +# \Method CSV.parse_line returns only the first row: |
| 161 | +# CSV.parse_line(string) # => ["foo", "0"] |
148 | 162 | #
|
149 |
| -# # From a string |
150 |
| -# arr_of_rows = CSV.parse("CSV,data,String", **options) |
151 |
| -# # or |
152 |
| -# CSV.parse("CSV,data,String", **options) do |row| |
153 |
| -# # ... |
154 |
| -# end |
| 163 | +# \CSV extends class \String with instance method String#parse_csv, |
| 164 | +# which also returns only the first row: |
| 165 | +# string.parse_csv # => ["foo", "0"] |
155 | 166 | #
|
156 |
| -# === Writing |
| 167 | +# ==== Parsing Via a \File Path |
157 | 168 | #
|
158 |
| -# # To a file |
159 |
| -# CSV.open("path/to/file.csv", "wb") do |csv| |
160 |
| -# csv << ["row", "of", "CSV", "data"] |
161 |
| -# csv << ["another", "row"] |
162 |
| -# # ... |
163 |
| -# end |
| 169 | +# The input to be parsed can be in a file: |
| 170 | +# string = "foo,0\nbar,1\nbaz,2\n" |
| 171 | +# path = 't.csv' |
| 172 | +# File.write(path, string) |
| 173 | +# |
| 174 | +# \Method CSV.read returns the entire \CSV data: |
| 175 | +# CSV.read(path) # => [["foo", "0"], ["bar", "1"], ["baz", "2"]] |
| 176 | +# |
| 177 | +# \Method CSV.foreach iterates, passing each row to the given block: |
| 178 | +# CSV.foreach(path) do |row| |
| 179 | +# p row |
| 180 | +# end |
| 181 | +# Output: |
| 182 | +# ["foo", "0"] |
| 183 | +# ["bar", "1"] |
| 184 | +# ["baz", "2"] |
| 185 | +# |
| 186 | +# \Method CSV.table returns the entire \CSV data as a CSV::Table object: |
| 187 | +# CSV.table(path) # => #<CSV::Table mode:col_or_row row_count:3> |
164 | 188 | #
|
165 |
| -# # To a String |
166 |
| -# csv_string = CSV.generate do |csv| |
167 |
| -# csv << ["row", "of", "CSV", "data"] |
168 |
| -# csv << ["another", "row"] |
169 |
| -# # ... |
| 189 | +# ==== Parsing from an Open \IO Stream |
| 190 | +# |
| 191 | +# The input to be parsed can be in an open \IO stream: |
| 192 | +# |
| 193 | +# \Method CSV.read returns the entire \CSV data: |
| 194 | +# File.open(path) do |file| |
| 195 | +# CSV.read(file) |
| 196 | +# end # => [["foo", "0"], ["bar", "1"], ["baz", "2"]] |
| 197 | +# |
| 198 | +# As does method CSV.parse: |
| 199 | +# File.open(path) do |file| |
| 200 | +# CSV.parse(file) |
| 201 | +# end # => [["foo", "0"], ["bar", "1"], ["baz", "2"]] |
| 202 | +# |
| 203 | +# \Method CSV.parse_line returns only the first row: |
| 204 | +# File.open(path) do |file| |
| 205 | +# CSV.parse_line(file) |
| 206 | +# end # => ["foo", "0"] |
| 207 | +# |
| 208 | +# \Method CSV.foreach iterates, passing each row to the given block: |
| 209 | +# File.open(path) do |file| |
| 210 | +# CSV.foreach(file) do |row| |
| 211 | +# p row |
| 212 | +# end |
170 | 213 | # end
|
| 214 | +# Output: |
| 215 | +# ["foo", "0"] |
| 216 | +# ["bar", "1"] |
| 217 | +# ["baz", "2"] |
| 218 | +# |
| 219 | +# \Method CSV.table returns the entire \CSV data as a CSV::Table object: |
| 220 | +# File.open(path) do |file| |
| 221 | +# CSV.table(file) |
| 222 | +# end # => #<CSV::Table mode:col_or_row row_count:3> |
| 223 | +# |
| 224 | +# === Simple Generating |
| 225 | +# |
| 226 | +# \Method CSV.generate returns a \String; |
| 227 | +# this example uses method CSV#<< to append the rows |
| 228 | +# that are to be generated: |
| 229 | +# output_string = CSV.generate do |csv| |
| 230 | +# csv << ['foo', 0] |
| 231 | +# csv << ['bar', 1] |
| 232 | +# csv << ['baz', 2] |
| 233 | +# end |
| 234 | +# output_string # => "foo,0\nbar,1\nbaz,2\n" |
| 235 | +# |
| 236 | +# \Method CSV.generate_line returns a \String containing the single row |
| 237 | +# constructed from an \Array: |
| 238 | +# CSV.generate_line(['foo', '0']) # => "foo,0\n" |
| 239 | +# |
| 240 | +# \CSV extends class \Array with instance method <tt>Array#to_csv</tt>, |
| 241 | +# which forms an \Array into a \String: |
| 242 | +# ['foo', '0'].to_csv # => "foo,0\n" |
171 | 243 | #
|
172 |
| -# === Shortcuts |
| 244 | +# === "Filtering" \CSV |
| 245 | +# |
| 246 | +# \Method CSV.filter provides a Unix-style filter for \CSV data. |
| 247 | +# The input data is processed to form the output data: |
| 248 | +# in_string = "foo,0\nbar,1\nbaz,2\n" |
| 249 | +# out_string = '' |
| 250 | +# CSV.filter(in_string, out_string) do |row| |
| 251 | +# row[0] = row[0].upcase |
| 252 | +# row[1] *= 4 |
| 253 | +# end |
| 254 | +# out_string # => "FOO,0000\nBAR,1111\nBAZ,2222\n" |
173 | 255 | #
|
174 |
| -# # Core extensions for converting one line |
175 |
| -# csv_string = ["CSV", "data"].to_csv # to CSV |
176 |
| -# csv_array = "CSV,String".parse_csv # from CSV |
| 256 | +# == \CSV Objects |
177 | 257 | #
|
178 |
| -# # CSV() method |
179 |
| -# CSV { |csv_out| csv_out << %w{my data here} } # to $stdout |
180 |
| -# CSV(csv = "") { |csv_str| csv_str << %w{my data here} } # to a String |
181 |
| -# CSV($stderr) { |csv_err| csv_err << %w{my data here} } # to $stderr |
182 |
| -# CSV($stdin) { |csv_in| csv_in.each { |row| p row } } # from $stdin |
| 258 | +# There are three ways to create a \CSV object: |
| 259 | +# - \Method CSV.new returns a new \CSV object. |
| 260 | +# - \Method CSV.instance returns a new or cached \CSV object. |
| 261 | +# - \Method \CSV() also returns a new or cached \CSV object. |
183 | 262 | #
|
184 |
| -# == Delegated Methods |
| 263 | +# === Delegated Methods |
185 | 264 | #
|
186 | 265 | # For convenience, a CSV object will delegate to many methods in class IO.
|
187 | 266 | # (A few have wrapper "guard code" in \CSV.) You may call:
|
|
219 | 298 | # * IO#truncate
|
220 | 299 | # * IO#tty?
|
221 | 300 | #
|
222 |
| -# == Options |
| 301 | +# === Options |
223 | 302 | #
|
224 | 303 | # The default values for options are:
|
225 | 304 | # DEFAULT_OPTIONS = {
|
|
249 | 328 | # strip: false,
|
250 | 329 | # }
|
251 | 330 | #
|
252 |
| -# === Options for Parsing |
| 331 | +# ==== Options for Parsing |
253 | 332 | #
|
254 | 333 | # :include: ../doc/options/common/col_sep.rdoc
|
255 | 334 | #
|
|
281 | 360 | #
|
282 | 361 | # :include: ../doc/options/parsing/empty_value.rdoc
|
283 | 362 | #
|
284 |
| -# === Options for Generating |
| 363 | +# ==== Options for Generating |
285 | 364 | #
|
286 | 365 | # :include: ../doc/options/common/col_sep.rdoc
|
287 | 366 | #
|
|
301 | 380 | #
|
302 | 381 | # :include: ../doc/options/generating/write_empty_value.rdoc
|
303 | 382 | #
|
304 |
| -# == CSV with headers |
| 383 | +# === \CSV with Headers |
305 | 384 | #
|
306 | 385 | # CSV allows to specify column names of CSV file, whether they are in data, or
|
307 | 386 | # provided separately. If headers are specified, reading methods return an instance
|
|
323 | 402 | # data = CSV.parse('Bob,Engineering,1000', headers: %i[name department salary])
|
324 | 403 | # data.first #=> #<CSV::Row name:"Bob" department:"Engineering" salary:"1000">
|
325 | 404 | #
|
326 |
| -# == \CSV \Converters |
| 405 | +# === \CSV \Converters |
327 | 406 | #
|
328 | 407 | # By default, each field parsed by \CSV is formed into a \String.
|
329 | 408 | # You can use a _converter_ to convert certain fields into other Ruby objects.
|
|
340 | 419 | # All \converters try to transcode fields to UTF-8 before converting.
|
341 | 420 | # The conversion will fail if the data cannot be transcoded, leaving the field unchanged.
|
342 | 421 | #
|
343 |
| -# === Field \Converters |
| 422 | +# ==== Field \Converters |
344 | 423 | #
|
345 | 424 | # There are three ways to use field \converters;
|
346 | 425 | # these examples use built-in field converter +:integer+,
|
|
431 | 510 | #
|
432 | 511 | # See {Custom Converters}[#class-CSV-label-Custom+Converters].
|
433 | 512 | #
|
434 |
| -# === Header \Converters |
| 513 | +# ==== Header \Converters |
435 | 514 | #
|
436 | 515 | # Header converters operate only on headers (and not on other rows).
|
437 | 516 | #
|
|
496 | 575 | #
|
497 | 576 | # See {Custom Converters}[#class-CSV-label-Custom+Converters].
|
498 | 577 | #
|
499 |
| -# === Custom \Converters |
| 578 | +# ==== Custom \Converters |
500 | 579 | #
|
501 | 580 | # You can define custom \converters.
|
502 | 581 | #
|
|
523 | 602 | # If the \converter does not need +field_info+, it can be omitted:
|
524 | 603 | # converter = proc {|field| ... }
|
525 | 604 | #
|
526 |
| -# == CSV and Character Encodings (M17n or Multilingualization) |
| 605 | +# === CSV and Character Encodings (M17n or Multilingualization) |
527 | 606 | #
|
528 | 607 | # This new CSV parser is m17n savvy. The parser works in the Encoding of the IO
|
529 | 608 | # or String object being read from or written to. Your data is never transcoded
|
@@ -1423,7 +1502,7 @@ def readlines(path, **options)
|
1423 | 1502 | # string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
1424 | 1503 | # path = 't.csv'
|
1425 | 1504 | # File.write(path, string)
|
1426 |
| - # CSV.table(path # => #<CSV::Table mode:col_or_row row_count:4> |
| 1505 | + # CSV.table(path) # => #<CSV::Table mode:col_or_row row_count:4> |
1427 | 1506 | def table(path, **options)
|
1428 | 1507 | default_options = {
|
1429 | 1508 | headers: true,
|
|
0 commit comments