On Tue, 29 Aug 2006, Mark Van H. wrote:
Is there any way to make the faster CSV library parse this line?
20 6" Multibrand Pricer Insert 2 4
I know i can use the :col_sep options to change the column separator to a
tab, but it fails to parse this because of an unclosed quoted field. It
seems like there should be an option to say that the fields are not quoted.
Thanks,
it that’s indeed the case why not simply do it yourself?
harp:~ > cat a.rb
require 'rubygems'
require 'fastercsv'
def munge line
line.gsub!(%r/"+/){|q| q.size % 2 == 0 ? q : '"' + q}
line.gsub!(%r/\ *\t\ */, '","')
"%s%s%s" % ['"', line, '"']
end
def show line
puts line
munged = munge line
puts munged
p(FCSV.parse(munged).first)
puts
end
lines = <<-lines
20 6" Multibrand Pricer Insert 2 4
20 6"" Multibrand Pricer Insert 2 4
20 6""" Multibrand Pricer Insert 2 4
20 6"""" Multibrand Pricer Insert 2 4
lines
lines.each{|line| show line.strip}
harp:~ > ruby a.rb
20 6" Multibrand Pricer Insert 2 4
"20","6""","Multibrand","Pricer","Insert","2","4"
["20", "6\"", "Multibrand", "Pricer", "Insert", "2", "4"]
20 6"" Multibrand Pricer Insert 2 4
"20","6""","Multibrand","Pricer","Insert","2","4"
["20", "6\"", "Multibrand", "Pricer", "Insert", "2", "4"]
20 6""" Multibrand Pricer Insert 2 4
"20","6""""","Multibrand","Pricer","Insert","2","4"
["20", "6\"\"", "Multibrand", "Pricer", "Insert", "2", "4"]
20 6"""" Multibrand Pricer Insert 2 4
"20","6""""","Multibrand","Pricer","Insert","2","4"
["20", "6\"\"", "Multibrand", "Pricer", "Insert", "2", "4"]
if fastercsv handled all the ‘simple’ exectptions is would be slow
and
complicated to maintain.
kind regards.
-a