Question

I've tried various iterations of :encoding and options with no success. The file is created using the python csv module with the quote_all option turned on. It uses the default quote_char = '"' and the encoding = 'ascii'; I have matched these settings in the ruby script but I get the following error whenever I try to read in this file:

/usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/csv.rb:1894:in `block (2 levels) in shift': Missing or stray quote in line 1 (CSV::MalformedCSVError)
    from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/csv.rb:1887:in `each'
    from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/csv.rb:1887:in `block in shift'
    from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/csv.rb:1849:in `loop'
    from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/csv.rb:1849:in `shift'
    from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/csv.rb:1791:in `each'
    from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/csv.rb:1805:in `to_a'
    from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/csv.rb:1805:in `read'
    from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/csv.rb:1411:in `block in read'
    from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/csv.rb:1354:in `open'
    from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/csv.rb:1411:in `read'

Here is line 1 of the csv file:

"1","

Because line 2 starts with a newline

Looks like this might have something to do with the source file; toggling some options enlightened me on what the problem may be.

Was it helpful?

Solution

I want to provide the answer that worked for me: I was unknowingly omitting some key formatting directives by removing the headers. Adding them back allowed the file to parse normally but I didn't want the headers to be processed by the parser so I had to find an option for doing that without disrupting the csv gem and I found that here: https://stackoverflow.com/a/20623102/2964789.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top