Skip to content

Instantly share code, notes, and snippets.

@alexec
Created August 3, 2013 10:21
Show Gist options
  • Save alexec/6146006 to your computer and use it in GitHub Desktop.
Save alexec/6146006 to your computer and use it in GitHub Desktop.
Example of reading and writing a CSV file
# suitable for small files only
require 'csv'
f='a.csv'
headers=['x','y']
csv=[]
if File.exists?(f)
CSV.foreach(f, {:headers => :first_row, :converters => [:numeric]}) do |row|
csv << row
end
end
csv << [rand(),rand()]
CSV.open(f, 'w') do |out|
out << headers
csv.each{|row| out << row}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment