Using CSV and STDIO?

18 views
Skip to first unread message

j...@via.net

unread,
Jun 5, 2015, 2:12:43 PM6/5/15
to rubyonra...@googlegroups.com



I would like to create a CSV filter, so I want to read from STDIN and write to STDOUT.


This does not work, even though this is per the the CSV web documentation:


require "rubygems"

require "csv"

require "nokogiri"



CSV($stdin, { :headers => true }){ |csv_in|  csv_in.each { |row| p row } }



/Users/joe/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/csv.rb:1871:in `block (2 levels) in shift': Unquoted fields do not allow \r or \n (line 1). (CSV::MalformedCSVError)

from /Users/joe/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/csv.rb:1836:in `each'

from /Users/joe/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/csv.rb:1836:in `block in shift'

from /Users/joe/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/csv.rb:1796:in `loop'

from /Users/joe/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/csv.rb:1796:in `shift'

from /Users/joe/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/csv.rb:1738:in `each'

from tt.rb:6:in `block in <main>'

from /Users/joe/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/csv.rb:1048:in `instance'

from /Users/joe/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/csv.rb:2320:in `CSV'

from tt.rb:6:in `<main>'



Any thought?

Scott Ribe

unread,
Jun 5, 2015, 3:03:53 PM6/5/15
to rubyonra...@googlegroups.com, j...@via.net
On Jun 5, 2015, at 12:12 PM, j...@via.net wrote:
>
> Any thought?

Like the error message says:

Unquoted fields do not allow \r or \n (line 1). (CSV::MalformedCSVError)

--
Scott Ribe
scott...@elevated-dev.com
http://www.elevated-dev.com/
https://www.linkedin.com/in/scottribe/
(303) 722-0567 voice





Scott Ribe

unread,
Jun 5, 2015, 4:56:00 PM6/5/15
to j...@via.net, rubyonra...@googlegroups.com
On Jun 5, 2015, at 2:50 PM, J...@via.net <j...@via.net> wrote:
>
> Well, this is an example straight from the documentation.
>
> I was hoping someone would post an example of using the csv class with stdio...

The error message is claiming that the data you're sending is malformed--that's not (directly) related to whether the data is coming from a file vs stdio.
Reply all
Reply to author
Forward
0 new messages