Skip to content

Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →

class method

CSV.parse

parse(string) → array_of_arrays
parse(io) → array_of_arrays
parse(string, headers: ..., **options) → csv_table
parse(io, headers: ..., **options) → csv_table
parse(string, **options) {|row| ... }
parse(io, **options) {|row| ... }

Parses string or io using the specified options.

  • Argument string should be a String object; it will be put into a new StringIO object positioned at the beginning.

  • Argument io should be an IO object that is:

    • Open for reading; on return, the IO object will be closed.

    • Positioned at the beginning. To position at the end, for appending, use method CSV.generate. For any other positioning, pass a preset StringIO object instead.

  • Argument options: see Options for Parsing

Without Option headers

Without option headers case.

These examples assume prior execution of:

Example
string = "foo,0\nbar,1\nbaz,2\n"
path = 't.csv'
File.write(path, string)

With no block given, returns an Array of Arrays formed from the source.

Parse a String:

Example
a_of_a = CSV.parse(string)
a_of_a # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]

Parse an open File:

Example
a_of_a = File.open(path) do |file|
  CSV.parse(file)
end
a_of_a # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]

With a block given, calls the block with each parsed row:

Parse a String:

Example
CSV.parse(string) {|row| p row }

Output:

Example
["foo", "0"]
["bar", "1"]
["baz", "2"]

Parse an open File:

Example
File.open(path) do |file|
  CSV.parse(file) {|row| p row }
end

Output:

Example
["foo", "0"]
["bar", "1"]
["baz", "2"]
With Option headers

With option headers case.

These examples assume prior execution of:

Example
string = "Name,Count\nfoo,0\nbar,1\nbaz,2\n"
path = 't.csv'
File.write(path, string)

With no block given, returns a CSV::Table object formed from the source.

Parse a String:

Example
csv_table = CSV.parse(string, headers: ['Name', 'Count'])
csv_table # => #<CSV::Table mode:col_or_row row_count:5>

Parse an open File:

Example
csv_table = File.open(path) do |file|
  CSV.parse(file, headers: ['Name', 'Count'])
end
csv_table # => #<CSV::Table mode:col_or_row row_count:4>

With a block given, calls the block with each parsed row, which has been formed into a CSV::Row object:

Parse a String:

Example
CSV.parse(string, headers: ['Name', 'Count']) {|row| p row }

Output:

Example
# <CSV::Row "Name":"foo" "Count":"0">
# <CSV::Row "Name":"bar" "Count":"1">
# <CSV::Row "Name":"baz" "Count":"2">

Parse an open File:

Example
File.open(path) do |file|
  CSV.parse(file, headers: ['Name', 'Count']) {|row| p row }
end

Output:

Example
# <CSV::Row "Name":"foo" "Count":"0">
# <CSV::Row "Name":"bar" "Count":"1">
# <CSV::Row "Name":"baz" "Count":"2">

Raises an exception if the argument is not a String object or IO object:

Example
# Raises NoMethodError (undefined method `close' for :foo:Symbol)
CSV.parse(:foo)
See also .open .parse_line .new .read also in 3.2 · 3.1 · 3.0