Skip to content

Instantly share code, notes, and snippets.

@HammerInTheNews
Created December 13, 2013 20:32
Show Gist options
  • Select an option

  • Save HammerInTheNews/7950871 to your computer and use it in GitHub Desktop.

Select an option

Save HammerInTheNews/7950871 to your computer and use it in GitHub Desktop.
Generate a CSV file – write rows to csvout.csv, read and print
require 'csv'
outfile = File.open('csvout.csv', 'wb')
CSV::Writer.generate(outfile) do |csv|
csv << ['c1', nil, 5]
csv << ['c2', 1, 2, 'abc']
end
outfile.close
#also read the file and print
require 'csv'
CSV::Reader.parse(File.open('csvout.csv', 'rb')) do |row|
# Process the row
p row[0].data # e.g. print the contents of the first cell
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment