I'm not totally sure I understand the question, but your test made it look like your data was one field you wanted to be able to read in. If so, you'll need to write is out properly escaped first:
class ConversionTest < Test::Unit::TestCase
def test_preserve_quoted_strings
field = '"string",2,0.3'
csv = [field].to_csv # => "\"\"\"string\"\",2,0.3\"\n"
assert_equal(field, csv.parse_csv.first)
end
end
If I'm wrong and you meant for that to be three separate fields, then just bust them up to get the valid CSV:
What I'm currently getting is "1storder" without the quotation marks.
I need the data fields to retain their quotation marks like they
have in the original CSV file.
Regards,
···
On Feb 26, 12:11 pm, James Gray <ja...@grayproductions.net> wrote:
On Feb 26, 2009, at 8:14 AM, BIl Kleb wrote:
> Google et al are failing me: How do I preserve quoted
> CSV strings on output?
I'm not totally sure I understand the question, but your test made it
look like your data was one field you wanted to be able to read in.
Quotes in CSV data are used to indicate field grouping. In other words, they are metadata about the content and it doesn't make sense for a parser to return those to you. It's like how an XML parser wouldn't give you the equals sign used to set a tag attribute.
The way I see it you have two choices:
1. Fix your data file so it's proper CSV (making the quotes a part of the field data). For example, the first row would become:
"""scheme""","""time_steps""","""dt"""
A quote is doubled to escape it in CSV and another set is added to enclose each field, which is why they are tripled here.
2. Decide that your data is not CSV and hand roll a parser to handle it. If you are sure fields won't contain commas, that may be a simple as: fields = row.split(",").
What I'm currently getting is "1storder" without the quotation marks.
I need the data fields to retain their quotation marks like they
have in the original CSV file.
On Feb 26, 2:08 pm, James Gray <ja...@grayproductions.net> wrote:
Well, then you don't really want a CSV parser.
Quotes in CSV data are used to indicate field grouping. In other
words, they are metadata about the content and it doesn't make sense
for a parser to return those to you. It's like how an XML parser
wouldn't give you the equals sign used to set a tag attribute.
On Feb 26, 2:08�pm, James Gray <ja...@grayproductions.net> wrote:
Well, then you don't really want a CSV parser.
Quotes in CSV data are used to indicate field grouping. �In other �
words, they are metadata about the content and it doesn't make sense �
for a parser to return those to you. �It's like how an XML parser �
wouldn't give you the equals sign used to set a tag attribute.
Ah, OK. That clears things up.
Thanks,
or use :force_quotes => true when FasterCSV.open or FasterCSV.new
--
Posted via http://www.ruby-forum.com/\.
That option causes FasterCSV to always quote fields on output. Bil was asking if he could have the quotes left in his fields on input.
James Edward Gray II
···
On Jul 9, 2009, at 5:59 AM, Marcus Mitchell wrote:
Bil Kleb wrote:
On Feb 26, 2:08�pm, James Gray <ja...@grayproductions.net> wrote:
Well, then you don't really want a CSV parser.
Quotes in CSV data are used to indicate field grouping. �In other �
words, they are metadata about the content and it doesn't make sense �
for a parser to return those to you. �It's like how an XML parser �
wouldn't give you the equals sign used to set a tag attribute.
Ah, OK. That clears things up.
Thanks,
or use :force_quotes => true when FasterCSV.open or FasterCSV.new