In Ruby 1.9 how do I read CSV from ARGF?
I tried the following, but it printed nothing:
require 'csv'
CSV(ARGF).read do |row|
p row
end
If you want lazy you could try:
CSV.new(ARGF.file).each do |row|
...
end
Source:
http://www.ruby-doc.org/stdlib-1.9.3/libdoc/csv/rdoc/CSV.html#label-Wrap+an+IO+Object
As it is said in http://www.ruby-doc.org/stdlib-1.9.3/libdoc/csv/rdoc/CSV.html#label-Wrap+an+IO+Object to use IO (like ARGF) you need to create a new cvs object:
csv = CSV.new(io, options)
An example like this one work fine:
#!/usr/bin/env ruby
#file : readCSV.rb
require "csv"
csv = CSV.new(ARGF, {:col_sep=>";", :headers=>:first_row})
csv.each do |line|
p line.to_s
end
It can then be use in two ways:
cat path/to/my.csv | ./readCSV.rb | less # for useless use of cat
./readCSV.rb < path/to/my.csv
./readCSV.rb path/to/my.csv | less
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With