I've been having an unbelievable time trying to import a CSV file in ruby-1.9.2.
The file I am trying to parse has:
- commas within columns
- quotes within columns
- uses an '@' as the :col_sep
csv.txt (representative input, real one is 101k lines):
?@?@jié@"seal" radical in Chinese characters, (Kangxi radical 26)
My code:
require 'csv'
CSV.foreach("/Users/adam/Desktop/csvtest.txt", {:col_sep => "@"}) do |row|
puts row.to_s
end
My desired output:
["?", "?", "jié", ""seal" radical in Chinese characters, (Kangxi radical 26)"]
What I get for output:
CSV::MalformedCSVError: Unclosed quoted field on line 1.
from /Users/adam/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/CSV.rb:1910:in `block in shift'
from /Users/adam/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/CSV.rb:1825:in `loop'
from /Users/adam/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/CSV.rb:1825:in `shift'
from /Users/adam/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/CSV.rb:1767:in `each'
from /Users/adam/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/CSV.rb:1202:in `block in foreach'
from /Users/adam/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/CSV.rb:1340:in `open'
from /Users/adam/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/CSV.rb:1201:in `foreach'
from (irb):31
from /Users/adam/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in `<main>'
It says there are unclosed quoted feilds, but I can see that the quotes open and close.
Escaping the quotes does nothing. I get the same error (...@""seal"" r...
).
Changing them to single quotes makes it work (...@'seal' r...
).
The problem is I NEED them to be in double quotes.
Any ideas?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…