News for hackers. Stuff that matters.
Re:Time to retire the CSV? by Twisell on Wednesday August 18, 2021 @11:18AM (#28222351)
OP and you gave me an idea :

"The only true successor of CSV should be forward/backward compatible with any existing CSV variant"

If we manage to write a spec that meet this criteria we'll have a powerful standard with easy adoption.

Re:Time to retire the CSV? by Macha on Wednesday August 18, 2021 @11:55AM (#28223057)
The issue is CSV is not compatible with CSV. It's not possible to write a spec that covers all CSV files in the world. CSV means things that are mutually incompatible in the less common cases, and the only way to really parse them correctly is to know which variant generated it. But you can't even tag that variant in the file by your criteria, as existing CSV parsers won't understand it.
Re:Time to retire the CSV? by godshatter on Wednesday August 18, 2021 @12:36PM (#28223665)
Couldn't tools that read CSV files scan them first and see which variant best matches the file? The questions arise about which end of line character(s) are used and how double-quotes and commas are handled. There can't be that many ways to escape them, and there are three sets of end of line characters mentioned - just see which one is used (i.e. don't assume only \n if you run into \r\n or \r alone).

The assumption that most software uses is that the import file will be in the same variant of the format as what that tool exports. That seems to be more of a problem than anything else.

Re:Time to retire the CSV? by masklinn on Wednesday August 18, 2021 @12:38PM (#28223692)
> Couldn't tools that read CSV files scan them first and see which variant best matches the file?

Sure, and they sometimes do that if they have to ingest CSVs whose origin they don't control (although not every system implementor cares enough to do it).

But that's still just a bunch of shitty faillible heuristics which would not be necessary if the format was not so horrible.

Re:Time to retire the CSV? by Macha on Wednesday August 18, 2021 @12:40PM (#28223719)
It also doesn't prevent a human or other system doing:

cat input1.csv input2.csv > output.csv

resulting in a single file containing multiple formats.

Also, what variant is this:

    1,5,Here is a string "" that does stuff,2021-1-1
What is the value of the third column?

Is this a CSV file without quoting? Then it's

    Here is a string "" that does stuff
Or is it a CSV file with double quote escaping? Then it's

    Here is a string " that does stuff
This is fundamentally undecidable without knowledge of what the format it is.

You can decide to just assume RFC compliant CSVs in the event of ambiguity, but then you absolutely will get bugs from users with non-RFC compliant CSV files.

Re:Time to retire the CSV? by godshatter on Wednesday August 18, 2021 @04:46PM (#28226885)
That's true. You could scan the file and see if there are any other types of double quote escaping happening, but if there isn't any that wouldn't help either. It's also negated by the multiple formats in the same file point.

So, yeah. Can't really be done without making too many assumptions that will break later.

Re:Time to retire the CSV? by hermitcrab on Wednesday August 18, 2021 @04:24PM (#28226608)
>Couldn't tools that read CSV files scan them first and see which variant best matches the file?

Yes. And my software does that. But it is always going to be a guess which the user needs to be able to override.

Re:Time to retire the CSV? by Twisell on Wednesday August 18, 2021 @12:33PM (#28223623)
By this argument UTF8 can't exist. And yet here it is.

PS: I never said 100% forward/backward compatible with all variant at the same time and without any noticeable artifact. I meant compatible in a non blocking way.

Re:Time to retire the CSV? by masklinn on Wednesday August 18, 2021 @12:37PM (#28223678)
What are you talking about? UTF8 is a single well-defined specification, and detecting that data is definitely not UTF8 is trivial.
Re:Time to retire the CSV? by Twisell on Wednesday August 18, 2021 @12:43PM (#28223766)
And yet it is forward/backward compatible with ASCII and non blocking against all it's ill defined variants.
Re:Time to retire the CSV? by Macha on Wednesday August 18, 2021 @12:49PM (#28223855)
ASCII was well defined, CSV was not. Therefore they could take the highest bit, which they could know that was unused per the ASCII spec, and use that to encode their extra UTF-8 information.

Also UTF-8/ascii compatibility is unidirectional. A tool that understands ASCII is going to print nonsense when it encounters emoji or whatever in UTF-8. Even the idea that tools that only understand ASCII won't mangle UTF-8 is limited - sure dumb passthroughs are fine, but if it manipulates the text at all, then you're out of luck - what does it mean to uppercase the first byte of a flag emoji?

Re:Time to retire the CSV? by tsimionescu on Wednesday August 18, 2021 @02:08PM (#28224966)
To be fair, there is basically no way to manipulate arbitrary text at all without mangling it, UTF-8-aware or not. What does it mean to take the first 7 characters of a UTF-8 string which might contain combinator characters and left-to-right special chars? What if the text uses special shaping chars, such as arranging hieroglyphs in cartouches? You basically need a text-rendering aware library to manipulate arbitrary strings.
Re:Time to retire the CSV? by masklinn on Wednesday August 18, 2021 @11:23AM (#28222440)
Given the infinity of CSV variants existing is the very reason why CSV blows goats, creating a spec compatible with that is

1. a fool's errand, CSV "variants" are not compatible with one another and regularly contradict one another (one needs not look any further than Excel's localised CSVs)

2. resulting in getting CSV anyway, which is a lot of efforts to do nothing

Re:Time to retire the CSV? by dragonwriter on Wednesday August 18, 2021 @12:11PM (#28223326)
> If we manage to write a spec that meet this criteria we'll have a powerful standard with easy adoption.

So, a binary format consisting of: (1) a text data segment (2) and end of file character (3) a second text data segment with structured metadata describing the layout of the first text data segment, which can be as simple (in terms of meaning; the structure should be more constrained for machine readability) as “It’s some kind of CSV, yo!” to a description of specific CSV variations (headers? column data types? escaping mechanisms? etc.) or even specify that the main body is JSON, YAML, XML, etc. (which would probably often be detectable by inspection, but this removes any ambiguity).

Re:Time to retire the CSV? by Twisell on Wednesday August 18, 2021 @12:22PM (#28223473)
You got my vibe

Almost any current CSV parser, even the bad ones, tolerate a header line.

So it should be possible to define a compact and standardized syntax that is appended before the real header of the first cell (separator,encoding,decimal separator (often disregarded by most parsers but crucial outside USA),quote character,escape character,etc...). Following headers would just use special notation to inform on (data-type,length,comment).

Newest parsers would use theses clues, older ones would just append some manageable junk to headers.

Re:Time to retire the CSV? by Macha on Wednesday August 18, 2021 @12:54PM (#28223941)
So someone opens this CSV in Excel and there's garbage in A1?

Does this really count as compatible? You will get user bugs for this.

Re:Time to retire the CSV? by dragonwriter on Wednesday August 18, 2021 @01:01PM (#28224079)
> So someone opens this CSV in Excel and there's garbage in A1?

Yeah, that's why I chose the “thing that looks like a text file—including optionally CSV—but has additional metadata after the EOF mark” approach instead of stuffing additional metadata in the CSV; there's no way to guarantee that existing implementations will safely ignore any added metadata the main CSV body. (My mechanism has some risk in that there are probably CSV readers that treat the file as a binary byte stream and use the file size rather than a text stream that ends at EOF, but I expect its far fewer than will do the wrong thing with additional metadata before the first header.

Re:Time to retire the CSV? by Macha on Wednesday August 18, 2021 @01:16PM (#28224321)
If by EOF char you mean Ctrl-Z, Python's `csv` module is at least one case where it will read past the EOF char and you'll get rows of garbage data for any content in the file after that.
Re:Time to retire the CSV? by sigzero on Wednesday August 18, 2021 @08:34PM (#28228995)
No, not binary.
Re:Time to retire the CSV? by ivanbakel on Wednesday August 18, 2021 @11:26AM (#28222503)
That's ignoring two big points in the OP: that CSV variants are ill-defined and conflicting; and that existing CSV data is too poorly-specified.

There is no reason to try to be "backwards-compatible" with existing CSV files - we don't have a single definition of correctness to use to check that the compatibility is correct. Every attempt to be parse existing data would result in unexpected results or even data loss for some CSVs in the wild, because there is no way to reconcile all the different expectations and specifications that people have for their own CSV data.

Re:Time to retire the CSV? by wernercd on Wednesday August 18, 2021 @12:02PM (#28223169)
Re:Time to retire the CSV? by sicariusnoctis on Wednesday August 18, 2021 @05:56PM (#28227671)
This is true. The best way would be if everyone generated CSV files that were compatible with on one or two already existing CSV formats. (e.g. no-quote-plain-old-csv or quotes-forced-csv.) Hopefully, it would be easy to distinguish between the two in some well-defined manner. Maybe call it .csvs (CSV standardized). Or just introduce new extensions like .csvq ("CSV quoted").

Note the difference is that I am suggesting reducing the number of standards in-use by using only one already existing CSV format. :)