Problem in reading txt file, by using CSV

I have switched my apps to Rails-3.0.2 & Ruby-1.9.2. I am reading .txt
file with tab separated. In the file, there is row like this,

[APPLE US USTR80302299 “Elmer Bernstein Sweet Smell of Success”
Mainstream Records USTR80302299]

Due to the double quotes in one of the column (“Elmer Bernstein Sweet
Smell of Success”), its generating error of :- Unclosed quoted field

@parsed_file =
CSV.open("#{RAILS_ROOT}/private/sales_report_files/#{@file_folder}/#{@sub_file_folder}/#{@file_name.filename}",‘r’)

This file is properly processed on Ruby-1.8.5 & Rails-2.3.8.I have tried
many solution, but its giving same error.

This works for me:

require ‘csv’

str =<<“END_OF_STRING”;
cow\tpig
hello\t"hello\tworld"\tworld
END_OF_STRING

CSV.parse(str, :col_sep => “\t”) do |row|
p row
end

–output:–
[“cow”, “pig”]
[“hello”, “hello\tworld”, “world”]

However, if I actually type the tabs, it doesn’t work:

require ‘csv’

str =<<“END_OF_STRING”;
cow pig
hello “hello world” world
END_OF_STRING

CSV.parse(DATA, :col_sep => “\t”) do |row|
p row
end

–output:–
/Users/me/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/csv.rb:1895:in
block (2 levels) in shift': Illegal quoting on line 1. (CSV::MalformedCSVError) from /Users/me/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/csv.rb:1863:ineach’
from
/Users/me/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/csv.rb:1863:in
block in shift' from /Users/me/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/csv.rb:1825:inloop’
from
/Users/me/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/csv.rb:1825:in
shift' from /Users/me/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/csv.rb:1767:ineach’
from
/Users/me/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/csv.rb:1370:in
parse' from ruby.rb:8:in

Due to the double quotes in one of the column

According to the CSV spec, double quotes are allowed around fields, so I
don’t know why FasterCSV, which replaced CSV in ruby 1.9, can’t handle
them.

2011/8/18 Gagan S. [email protected]:

@parsed_file =

CSV.open(“#{RAILS_ROOT}/private/sales_report_files/#{@file_folder}/#{@sub_file_folder}/#{@file_name.filename}”,‘r’)

You’re not setting the :col_sep option, so CSV assumes the separator
is a comma (while in the file it’s clearly a space). It worked for me:

irb(main):004:0> CSV.open(‘data.txt’, col_sep:’ ').read
=> [[“APPLE”, “US”, “USTR80302299”, “Elmer Bernstein Sweet Smell of
Success”, “Mainstream”, “Records”, “USTR80302299”]]

The error you got is quite cryptic, but it makes sense: you had a
single column, which contained quotes not at the beginning or end -
CSV parser considers this incorrect.

– Matma R.