-1

I'm currently working on a report application. I can create CSV files to hold my data but I would like to add metadata to each row in order to do future grouping, categorize, etc.

I'm not aware what the proper syntax for metadata is in a CSV file but I've seen someone do:

# publisher City of Palo Alto
# updated 12/31/2010
# name GID on_street species trim_cycle  inventory_date
# datatype string  string  string  string  date:M/D/YYYY
  GID, On Street, Species, Trim Cycle  ,Inventory, Date
  1, ADDISON AV,  Celtis australis,  Large Tree, Routine Prune,  10/18/2010
  2, EMERSON ST,  Liquidambar styraciflua, Large Tree, Routine Prune,  6/2/2010

I attempted that and the data after # was still shown as a field when the CSV file was opened up.

I'm creating my CSV file using Ruby.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Brinny
  • 11
  • 1
  • 1
    You can't add anything besides data to a CSV file. It's entirely data. You _could_ define a field that has a string value, but comments aren't allowed. – the Tin Man Nov 22 '19 at 05:28
  • And, if you saw "someone do something", they weren't using a CSV parser. Either they were preprocessing the file to strip the comment lines, or they were reading it line by line and parsing the fields. CSV is a simple format, but it does have some common rules. – the Tin Man Nov 22 '19 at 05:43
  • Does this answer your question? [Store metadata in CSV file](https://stackoverflow.com/questions/30739931/store-metadata-in-csv-file) – Ruminateer May 14 '22 at 08:13

1 Answers1

2

CSV stays for “Comma Separated Values”. It’s roughly a text format, containing records, one record per row, fields separated by commas (in some cases other delimiters are allowed, like semicolon ; and/or tab \t,) with the first row possibly treated as headers (still being comma-separated values in a row.)

CSV format does not support comments, nor metadata, nor anything else.

The only way to support whatever you need beyond the above functionality is to manually parse the file with your own parser. All the CSV readers / writers but yours would effectively treat these rows as normal rows, trying to parse them as comma-separated fields.

The proper approach would be to store metadata in the other file near your CSV, or create an additional field for the metadata.

Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
  • 1
    CSV isn't a "roughly" format, it's text. "[A comma-separated values (CSV) file is a delimited text file](https://en.wikipedia.org/wiki/Comma-separated_values)...". Non-text can be encoded into a text-only format. Trying to import a binary file as text will blow up badly because the delimiter could appear arbitrarily in the content. The header row can be any of the separator delimited options as long as it matches the rest of the file's delimiters. CSV is _always_ comma-separated. Alternate separators are allowed but they're not "CSV" because CSV is the catch-all term. – the Tin Man Nov 22 '19 at 05:39
  • Did this ever see the light of day or will it? https://www.w3.org/TR/tabular-data-model/ – lacostenycoder Nov 22 '19 at 06:21