James Gray
2/26/2009 7:09:00 PM
On Feb 26, 2009, at 12:44 PM, Bil Kleb wrote:
> I have a CSV file with headers and rows like
>
> "scheme","time_steps","dt"
> "1storder",2,0.5
> "4thorder",5,1.0
>
> and I am using FasterCSV to read this CSV file to get
> a hash of header=>value pairs for each row.
>
> For each row worth of data, I create an output file of the form
>
> &some_weird_name
> scheme = "1storder",
> time_steps = 2,
> dt = 0.5
> /
>
> 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.
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.
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(",").
Hope that helps.
James Edward Gray II