Forum: Ruby Changing the quote-character in csv parsing

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1bc028840986928fb298a9eb456dbb22?d=identicon&s=25 Jens Auer (Guest)
on 2006-03-28 17:04
(Received via mailing list)
Hi,
I have a bunch of files containing lines as comma-seperated values.
Unfortunately, the character used for quoting is a single quote (') and
not the double quote ("). How can I tell the csv library (or fastercsv
or any other csv-parsing library) which character is used for quoting?
Some of the files contain fields like 'quoted, but with comma', which
are seperated into two fields at the comma:
irb(main):003:0> line = "one, 'quoted', 'quoted, but with comma'"
=> "one, 'quoted', 'quoted, but with comma'"
irb(main):006:0> CSV::parse_line('some words "some quoted text" some
more words', ' ')
=> ["some", "words", "some quoted text", "some", "more", "words"]
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require_gem 'fastercsv'
=> true
irb(main):004:0> line.parse_csv
=> ["one", " 'quoted'", " 'quoted", " but with comma'"]

The output should be ["one", "'quoted'", "'quoted, but with comma'"]

I already have searched the rdoc for the csv library without any
success.
E055e58f939e78351414a19c795c3061?d=identicon&s=25 Jan Topinski (Guest)
on 2006-03-28 18:09
(Received via mailing list)
Hi,
Don't know how it is with fastercsv but csv.rb has double quote
hardcoded as
I understand. I think best is to substitute all double quots in your
text
with single and vice versa. You can do it with gsub this way:


line.gsub!(/'|\"/){ |c|
  if c == "'"
    "\""
  else
    "'"
  end
}

jan
2ee1a7960cc761a6e92efb5000c0f2c9?d=identicon&s=25 William James (Guest)
on 2006-03-28 18:09
(Received via mailing list)
Jan Topinski wrote:
>     "'"
>   end
> }

"He said, \"I don't care.\"".tr("'\"", "\"'")
E055e58f939e78351414a19c795c3061?d=identicon&s=25 Jan Topinski (Guest)
on 2006-03-28 18:19
(Received via mailing list)
ups me blind ;)
jan
This topic is locked and can not be replied to.