-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathcsv.cr
451 lines (406 loc) · 13.8 KB
/
csv.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
# Provides methods and classes for parsing and generating CSV
# (comma-separated values) strings.
#
# This module conforms to [RFC 4180](https://tools.ietf.org/html/rfc4180).
#
# NOTE: To use `CSV` or its children, you must explicitly import it with `require "csv"`
#
# ### Parsing
#
# Several ways of parsing CSV are provided. The most straight-forward, but
# slow or inefficient for some scenarios, is `CSV#parse`, which returns
# an array of arrays of all data.
#
# Rows can be traversed in a linear fashion with `CSV#each_row`, or
# using an `Iterator`.
#
# To parse a CSV in an efficient way, optionally being able to access
# row values from header names, create an instance of a `CSV`.
#
# ### Parsing with `CSV#new`
#
# A CSV instance holds a cursor to the current row in the CSV. The cursor
# is advanced by invoking `#next`, which returns `true` if a next row was found,
# and `false` otherwise. A first call to `#next` is required to position the
# CSV parser in the first row.
#
# Once positioned in a row, values can be obtained with the several `#[]` methods,
# which can accept a header name, column position, or header name pattern as a `Regex`.
#
# Additionally, a `Row` object can be obtained with the `#row` method which
# provides similar methods and can be converted to an `Array` or `Hash`.
#
# ### Example
#
# ```
# require "csv"
#
# csv = CSV.new("Name, Age\nJohn, 20\nPeter, 30", headers: true)
# csv.next # => true
#
# csv["Name"] # => "John"
# csv[0] # => "John"
# csv[-2] # => "John"
# csv[/name/i] # => "John"
#
# csv["Age"] # => " 20"
#
# csv.row.to_a # => ["John", " 20"]
# csv.row.to_h # => {"Name" => "John", "Age" => " 20"}
#
# csv.next # => true
# csv["Name"] # => "Peter"
#
# csv.next # => false
# ```
#
# ### Building
#
# To create CSV data, check `CSV#build` and the `CSV::Builder` class.
class CSV
DEFAULT_SEPARATOR = ','
DEFAULT_QUOTE_CHAR = '"'
# Parses a CSV or `IO` into an array.
#
# Takes optional *separator* and *quote_char* arguments for defining
# non-standard csv cell separators and quote characters.
#
# ```
# require "csv"
#
# CSV.parse("one,two\nthree")
# # => [["one", "two"], ["three"]]
# CSV.parse("one;two\n'three;'", separator: ';', quote_char: '\'')
# # => [["one", "two"], ["three;"]]
# ```
def self.parse(string_or_io : String | IO, separator : Char = DEFAULT_SEPARATOR, quote_char : Char = DEFAULT_QUOTE_CHAR) : Array(Array(String))
Parser.new(string_or_io, separator, quote_char).parse
end
# Yields each of a CSV's rows as an `Array(String)`.
#
# See `CSV.parse` about the *separator* and *quote_char* arguments.
#
# ```
# require "csv"
#
# CSV.each_row("one,two\nthree") do |row|
# puts row
# end
# ```
#
# Output:
#
# ```
# ["one", "two"]
# ["three"]
# ```
def self.each_row(string_or_io : String | IO, separator : Char = DEFAULT_SEPARATOR, quote_char : Char = DEFAULT_QUOTE_CHAR, &)
Parser.new(string_or_io, separator, quote_char).each_row do |row|
yield row
end
end
# Returns an `Iterator` of `Array(String)` over a CSV's rows.
#
# See `CSV.parse` about the *separator* and *quote_char* arguments.
#
# ```
# require "csv"
#
# rows = CSV.each_row("one,two\nthree")
# rows.next # => ["one", "two"]
# rows.next # => ["three"]
# ```
def self.each_row(string_or_io : String | IO, separator : Char = DEFAULT_SEPARATOR, quote_char : Char = DEFAULT_QUOTE_CHAR)
Parser.new(string_or_io, separator, quote_char).each_row
end
# Builds a CSV. This yields a `CSV::Builder` to the given block.
#
# Takes optional *quoting* argument to define quote behavior.
#
# ```
# require "csv"
#
# result = CSV.build do |csv|
# csv.row "one", "two"
# csv.row "three"
# end
# result # => "one,two\nthree\n"
# result = CSV.build(quoting: CSV::Builder::Quoting::ALL) do |csv|
# csv.row "one", "two"
# csv.row "three"
# end
# result # => "\"one\",\"two\"\n\"three\"\n"
# ```
#
# See: `CSV::Builder::Quoting`
def self.build(separator : Char = DEFAULT_SEPARATOR, quote_char : Char = DEFAULT_QUOTE_CHAR, quoting : Builder::Quoting = Builder::Quoting::RFC, &) : String
String.build do |io|
build(io, separator, quote_char, quoting) { |builder| yield builder }
end
end
# Appends CSV data to the given `IO`. This yields a `CSV::Builder`
# that writes to the given `IO`.
#
# ```
# require "csv"
#
# io = IO::Memory.new
# io.puts "HEADER"
# CSV.build(io) do |csv|
# csv.row "one", "two"
# csv.row "three"
# end
# io.to_s # => "HEADER\none,two\nthree\n"
# ```
def self.build(io : IO, separator : Char = DEFAULT_SEPARATOR, quote_char : Char = DEFAULT_QUOTE_CHAR, quoting : Builder::Quoting = Builder::Quoting::RFC, &) : Nil
builder = Builder.new(io, separator, quote_char, quoting)
yield builder
io.flush
end
@headers : Array(String)?
@indices : Hash(String, Int32)?
# Creates a new instance from the given `String` or `IO`.
#
# * If *strip* is `true`, row values are stripped with `String#strip` before being
# returned from methods.
# * If *headers* is `true`, row values can be accessed with header names or patterns.
# Headers are always stripped.
#
# See `CSV.parse` about the *separator* and *quote_char* arguments.
def initialize(string_or_io : String | IO, headers = false, @strip = false, separator : Char = DEFAULT_SEPARATOR, quote_char : Char = DEFAULT_QUOTE_CHAR)
@parser = Parser.new(string_or_io, separator, quote_char)
if headers
headers = @parser.next_row || ([] of String)
headers = @headers = headers.map &.strip
indices = @indices = {} of String => Int32
headers.each_with_index do |header, index|
indices.put_if_absent(header, index)
end
end
@traversed = false
end
# Creates a new instance from the given `String` or `IO`, and yields it to
# the given block once for each row in the CSV.
#
# * If *strip* is `true`, row values are stripped with `String#strip` before being
# returned from methods.
# * If *headers* is `true`, row values can be accessed with header names or patterns.
# Headers are always stripped.
#
# See `CSV.parse` about the *separator* and *quote_char* arguments.
def self.new(string_or_io : String | IO, headers = false, strip = false, separator : Char = DEFAULT_SEPARATOR, quote_char : Char = DEFAULT_QUOTE_CHAR, &)
csv = new(string_or_io, headers, strip, separator, quote_char)
csv.each do
yield csv
end
csv
end
# Returns this CSV headers. Their values are always stripped.
# Raises `CSV::Error` if headers were not requested.
def headers : Array(String)
@headers || raise(Error.new("Headers not requested"))
end
# Invokes the block once for each row in this CSV, yielding `self`.
def each(&) : Nil
while self.next
yield self
end
end
# Advanced the cursor to the next row. Must be called once to position
# the cursor in the first row. Returns `true` if a next row was found,
# `false` otherwise.
def next : Bool
return false if @traversed
row = @row ||= [] of String
row.clear
@row = @parser.next_row(row)
if @row
true
else
@traversed = true
false
end
end
# Returns the current row's value corresponding to the given *header* name.
# Raises `KeyError` if no such header exists.
# Raises `CSV::Error` if headers were not requested.
def [](header : String) : String
row_internal[header]
end
# Returns the current row's value corresponding to the given *header* name.
# Returns `nil` if no such header exists.
# Raises `CSV::Error` if headers were not requested.
def []?(header : String) : String?
row_internal[header]?
end
# Returns the current row's value at the given column index.
# A negative index counts from the end.
# Raises `IndexError` if no such column exists.
def [](column : Int) : String
row_internal[column]
end
# Returns the current row's value at the given column index.
# A negative index counts from the end.
# Returns `nil` if no such column exists.
def []?(column : Int) : String?
row_internal[column]?
end
# Returns the current row's value corresponding to the given *header_pattern*.
# Raises `KeyError` if no such header exists.
# Raises `CSV::Error` if headers were not requested.
def [](header_pattern : Regex, *, options : Regex::MatchOptions = Regex::MatchOptions::None) : String
row_internal[header_pattern, options: options]
end
# Returns the current row's value corresponding to the given *header_pattern*.
# Returns `nil` if no such header exists.
# Raises `CSV::Error` if headers were not requested.
def []?(header_pattern : Regex, *, options : Regex::MatchOptions = Regex::MatchOptions::None) : String?
row_internal[header_pattern, options: options]?
end
# Returns a tuple of the current row's values at given indices
# A negative index counts from the end.
# Raises `IndexError` if any column doesn't exist
# The behavior of returning a tuple is similar to `Hash#values_at`
def values_at(*columns : Int)
columns.map { |column| row_internal[column] }
end
# Returns a tuple of the current row's values corresponding to the given *headers*
# Raises `KeyError` if any header doesn't exist.
# Raises `CSV::Error` if headers were not requested
# The behavior of returning a tuple is similar to `Hash#values_at`
def values_at(*headers : String)
headers.map { |header| row_internal[header] }
end
# Returns the current row as a `Row` instance.
def row : Row
Row.new(self, current_row.dup)
end
# Rewinds this CSV to the beginning, rewinding the underlying IO if any.
def rewind : Nil
@parser.rewind
@parser.next_row if @headers
@traversed = false
end
private def row_internal
Row.new(self, current_row)
end
private def current_row
row = @row
return row if row
if @traversed
raise Error.new("After last row")
else
raise Error.new("Before first row")
end
end
# :nodoc:
def indices : Hash(String, Int32)
@indices || raise(Error.new("Headers not requested"))
end
# :nodoc:
getter? strip
# :nodoc:
def headers? : Array(String)?
@headers
end
# A Row of a `CSV::WithHeaders` instance.
struct Row
private getter csv : CSV
@row : Array(String)
# :nodoc:
def initialize(@csv, @row)
end
# Returns the current row's value corresponding to the given *header* name.
# Raises `KeyError` if no such header exists.
# Raises `CSV::Error` if headers were not requested.
def [](header : String) : String
value = self.[]?(header)
raise KeyError.new("Missing header: #{header}") unless value
value
end
# Returns this row's value corresponding to the given *header* name.
# Returns `nil` if no such header exists.
# Raises `CSV::Error` if headers were not requested.
def []?(header : String) : String?
index = csv.indices[header]?
if index
maybe_strip(@row[index]? || "")
else
nil
end
end
# Returns this row's value at the given column index.
# A negative index counts from the end.
# Raises `IndexError` if no such column exists.
def [](column : Int) : String
value = self.[]?(column)
raise IndexError.new("Missing column index: #{column}") unless value
value
end
# Returns this row's value at the given column index.
# A negative index counts from the end.
# Returns `nil` if no such column exists.
def []?(column : Int) : String?
size = csv.headers?.try(&.size) || @row.size
column += size if column < 0
value = @row[column]?
value ||= "" if 0 <= column < size
value ? maybe_strip(value) : nil
end
# Returns this row's value corresponding to the given *header_pattern*.
# Raises `KeyError` if no such header exists.
# Raises `CSV::Error` if headers were not requested.
def [](header_pattern : Regex, *, options : Regex::MatchOptions = Regex::MatchOptions::None) : String
value = self.[]?(header_pattern, options: options)
raise KeyError.new("Missing header pattern: #{header_pattern}") unless value
value
end
# Returns this row's value corresponding to the given *header_pattern*.
# Returns `nil` if no such header exists.
# Raises `CSV::Error` if headers were not requested.
def []?(header_pattern : Regex, *, options : Regex::MatchOptions = Regex::MatchOptions::None) : String?
csv.headers.each_with_index do |header, i|
if header.matches?(header_pattern, options: options)
return maybe_strip(@row[i]? || "")
end
end
nil
end
# Returns the number of columns in this row, regardless of the number
# of headers (if requested).
def size : Int32
@row.size
end
# Converts this row to an `Array`.
def to_a : Array(String)
@row.map!(&.strip) if csv.strip?
@row
end
# Converts this row to a `Hash`.
def to_h : Hash(String, String)
h = {} of String => String
csv.headers.each_with_index do |header, i|
h[header] = maybe_strip(@row[i]? || "")
end
h
end
# Returns a tuple of this row's values at given indices
# A negative index counts from the end.
# Raises `IndexError` if any column doesn't exist
# The behavior of returning a tuple is similar to `Hash#values_at`
def values_at(*columns : Int)
columns.map { |column| self[column] }
end
# Returns a tuple of this row's values corresponding to the given *headers*
# Raises `KeyError` if any header doesn't exist.
# Raises `CSV::Error` if headers were not requested
# The behavior of returning a tuple is similar to `Hash#values_at`
def values_at(*headers : String)
headers.map { |header| self[header] }
end
private def maybe_strip(value)
csv.strip? ? value.strip : value
end
end
end
require "./csv/**"