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.
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.
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.
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/.
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