For other recipes, see Recipes for CSV.
All code snippets on this page assume that the following has been executed:
require 'csv'
You can use a Unix-style “filter” for CSV data. The filter reads source CSV data and writes output CSV data as modified by the filter. The input and output CSV data may be any mixture of Strings and IO streams.
You can filter one String to another, with or without headers.
Use class method CSV.filter with option headers
to filter a String to another String:
in_string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n" out_string = '' CSV.filter(in_string, out_string, headers: true) do |row| row[0] = row[0].upcase row[1] *= 4 end out_string # => "Name,Value\nFOO,0000\nBAR,1111\nBAZ,2222\n"
Use class method CSV.filter without option headers
to filter a String to another String:
in_string = "foo,0\nbar,1\nbaz,2\n" out_string = '' CSV.filter(in_string, out_string) do |row| row[0] = row[0].upcase row[1] *= 4 end out_string # => "FOO,0000\nBAR,1111\nBAZ,2222\n"
You can filter a String to an IO stream, with or without headers.
Use class method CSV.filter with option headers
to filter a String to an IO stream:
in_string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n" path = 't.csv' File.open(path, 'w') do |out_io| CSV.filter(in_string, out_io, headers: true) do |row| row[0] = row[0].upcase row[1] *= 4 end end p File.read(path) # => "Name,Value\nFOO,0000\nBAR,1111\nBAZ,2222\n"
Use class method CSV.filter without option headers
to filter a String to an IO stream:
in_string = "foo,0\nbar,1\nbaz,2\n" path = 't.csv' File.open(path, 'w') do |out_io| CSV.filter(in_string, out_io) do |row| row[0] = row[0].upcase row[1] *= 4 end end p File.read(path) # => "FOO,0000\nBAR,1111\nBAZ,2222\n"
You can filter an IO stream to a String, with or without headers.
Use class method CSV.filter with option headers
to filter an IO stream to a String:
in_string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n" path = 't.csv' File.write(path, in_string) out_string = '' File.open(path, headers: true) do |in_io| CSV.filter(in_io, out_string, headers: true) do |row| row[0] = row[0].upcase row[1] *= 4 end end out_string # => "Name,Value\nFOO,0000\nBAR,1111\nBAZ,2222\n"
Use class method CSV.filter without option headers
to filter an IO stream to a String:
in_string = "foo,0\nbar,1\nbaz,2\n" path = 't.csv' File.write(path, in_string) out_string = '' File.open(path) do |in_io| CSV.filter(in_io, out_string) do |row| row[0] = row[0].upcase row[1] *= 4 end end out_string # => "FOO,0000\nBAR,1111\nBAZ,2222\n"
You can filter an IO stream to another IO stream, with or without headers.
Use class method CSV.filter with option headers
to filter an IO stream to another IO stream:
in_path = 't.csv' in_string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n" File.write(in_path, in_string) out_path = 'u.csv' File.open(in_path) do |in_io| File.open(out_path, 'w') do |out_io| CSV.filter(in_io, out_io, headers: true) do |row| row[0] = row[0].upcase row[1] *= 4 end end end p File.read(out_path) # => "Name,Value\nFOO,0000\nBAR,1111\nBAZ,2222\n"
Use class method CSV.filter without option headers
to filter an IO stream to another IO stream:
in_path = 't.csv' in_string = "foo,0\nbar,1\nbaz,2\n" File.write(in_path, in_string) out_path = 'u.csv' File.open(in_path) do |in_io| File.open(out_path, 'w') do |out_io| CSV.filter(in_io, out_io) do |row| row[0] = row[0].upcase row[1] *= 4 end end end p File.read(out_path) # => "FOO,0000\nBAR,1111\nBAZ,2222\n"