by geocar · 5y ago · 0 comments
- whether quotes are ""doubled like SQL92 or backslash-\"escaped like C
- whether newlines are quoted or \n like C
- whether there are column headings
- what the character set of the file is (Excel insists it's the local machine's codepage)
- whether that's a BOM or part of a column heading
- whether lines terminate with \n or \r\n or \r or \r\n
- what to do about ragged rows
and so on. Except for the simplest of datasets, CSV is almost certainly going to lose information. The only advantages over SQLite is streaming support and maybe compression, but even then there are better formats.
I would say any format that is compatible with Excel is not suitable for archival. Ever open a file with numeric values with more than say, 20 (I forget the actual cut off) digits? Excel converts to scientific notation and loses precision. When you change the format there is data loss. The length data is preserved but not the full precision.
So you have to close it and use Data Import.
We just moved to the latest version of Office 365 at the job and it still works like this (even if the import action can usually figure out that semicolon is the separator without me having to manually specify it...).
~ is a better choice for field separator though, it is far less commonly used in most datasets and natural languages. | can also make sense for the same reason, and visually looks like a column divider as an extra bonus.
I don't know where there's never been some effort to create a "CSV format specification"; for example the first line could indicate quoting, delimiter, etc. style used in the file.
Guess it's never been big enough of a problem for people to take action. "Good enough" (or rather, "not bad enough").
Or a pipe. Or the unprintable “field separator” character.
> I don't know where there's never been some effort to create a "CSV format specification"; for example the first line could indicate quoting, delimiter, etc. style used in the file.
Fundamentally CSV or any delimited format requires agreement between parties for interchange. At that point standards don’t really help because the parties can just agree to anything that works.
> Guess it's never been big enough of a problem for people to take action. "Good enough" (or rather, "not bad enough").
Exactly.
RFC 4180 (https://tools.ietf.org/html/rfc4180) "Common Format and MIME Type for Comma-Separated Values (CSV) Files"
$ cat file.csv
CSV,quote=always,quotechar=",header=yes,delim=,
Col1,Col2
"val","val2"
[..]
Then you always know what you're dealing with, unambiguously.There are other cases where embedding some "metadata" in CSV files is useful; for example the exports I create in my app are versioned by prefixing the first header with the version number. It works, but it's not exactly great.
Of course, the downside is legacy software not being able to process it.
Jokes aside, we had that debate in our team way back and decided to follow the rfc and only use commas. The format is often abused, better not create more tools that do so.
Of course, there might be programs that write invalid CSV or parsers that accept invalid CSV, but that doesn’t mean CSV isn’t a standardised format.
Like Excel?
RFC 4180 exists but that doesn't mean any file with .csv at the end of the name or a text/csv MIME type can be parsed according to it. Also, you still have to track the metadata, such as the charset and if the first line is a header. Both are optional in the RFC.
e: RFC 4180 is not a standard.
The character set is also forgivable given the age of the format; it comes from an era when there wasn’t an agreed single character encoding to rule them all. Sure, RFC 4180 probably should have specified UTF-8 but by that point there was already three decades of CSV usage in different encodings on mainframes which are likely still in use even now to make specifying any character encoding rather pointless.
Don’t get me wrong, I’m not a massive fan of CSV either. The way quoted new lines are encoded, for example, is horrible and I’ve never liked the “” format for escaping quotation marks. But a lot of CSVs biggest problems are ironically a result of its success: as a format it’s so easy to use that a great many developers can hand crank their own parsers without looking at the spec. So I find it hard being critical about the lack of standardisation in CSV when the real problem is the number of developers who don’t follow the standardisation.
RFC4180 isn't an Internet standard. Says so right at the top in the first paragraph.
I think I have a few thousand files on my computer right now whose names end in ".csv", and I'll bet money not one of them agrees with RFC4180 to the letter except by accident of the data itself, and that's a Real Problem to me.
I can concede that two parties could agree to interchange according to RFC4180, but as a general format I maintain that for archival and interchange purposes CSV cannot be divorced from the rather complex schema I alluded to without data loss.
Quotes are doubled
Newlines are quoted
Both having headings and not having headings is valid. Document/data specific.
Use utf8
Do not use bom at all. But if for whatever reason you must use it in column heading, quote it.
Idk which line ending but you should probably handle both
Do not emit ragged rows.