Chaning the quote-character in csv parsing

J

Jens Auer

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'"]
 
J

James Edward Gray II

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?

I'm not aware of a way to change it for either library, sadly. If
your data is simple, you might get away with manipulating it into shape:
=> ["one", "quoted", "quoted, but with comma"]

Unfortunately, on of the examples you showed is more complicated than
that:
=> ["some words \"some quoted text\" some more words"]

Those transforms need to be done on a field by field basis though, to
correctly convert data like that.

Hope that helps.

James Edward Gray II
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top