Comma Separated Values (CSV) is a simple plain text format for storing tabular data. To create a CSV file, just write every row of your data as a line in your file, and separate columns using a comma:
name,age,favorite food
Beto,48,hummus
Martim,11,hamburgerIt's trivial to parse. For example, here's an example in Python:
for row in open(filename):
for column in row.strip().split(","):
print(column)Easy. Or is it?
What if Martim's favorite food was hamburger, fries, and soda? Now we need to use quotes to represent the cell value:
name,age,favorite food
Beto,48,hummus
Martim,11,"hamburger, fries, and soda"Parsing is now harder, since we need to keep track of opening and closing quotes. We need to strip the quotes as well, so that the value is hamburger, fries, and soda, instead of "hamburger, fries, and soda". But what if one of the cells has quotes? We need to preserve those!
As you can see, it quickly becomes complicated. Combined with the fact that there's no official specification, parsing CSV files can be a nightmare.
A nice alternative is CCSV: Control Character Separated Values. CCSV files use 3 characters that are non-printable, used only for control:
ASCII 30: record separator (␞)
ASCII 31: unit separator (␟)
ASCII 4: end-of-transmission (␄)
The advantage of using these characters instead of line breaks and commas is that they are very VERY unlikely to appear in your data. When parsing a CCSV file you don't need to handle special cases. Our CSV file would look like this:
name␟age␟favorite food␞
Beto␟48␟hummus␞
Martim␟11␟hamburger, fries, and soda␄And to parse:
for row in open(filename).read().rstrip(chr(4)).split(chr(30)):
for column in row.split(chr(31)):
print(column)I've been using the format with my new Gforth blog, since it's trivial to generate and parse. But as the CCSV website rightly calls out, there are drawbacks:
The delimiters used in .ccsv files are generally not visible in an ordinary text editor. Editing the files by hand can be difficult, if not impossible, using an editor that is unaware of the file format.
The thing I like the most about CCSV files is that it uses an END OF TRANSMISSION character at the end. How cool is that?
␄