News for hackers. Stuff that matters.
Re:So you want to write your own CSV code (2014) by cesaref on Friday May 15, 2020 @07:08AM (#23190800)
One thing that has always puzzled me is why CSV exists, given the ASCII unit/record seperators appear to do exactly what the comma and newline characters are attempting to do with codes which could appear within records, and have been well defined since some point in the 60s.

And here we are, 60 years later still struggling to work out where a record ends...

Re:So you want to write your own CSV code (2014) by atoav on Friday May 15, 2020 @07:27AM (#23190918)
Easy: CSVs are human readable and writeable and humans tend to have a comma sign on their keyboard, while they don't have the ASCII record seperator visible (teaching them to use eg Alt + 30 will not work).

And if they had that key on their keyboard then you'd have the comma problems all over again: What if a ASCII record seperator shows up in the field?

Re:So you want to write your own CSV code (2014) by r00fus on Friday May 15, 2020 @12:41PM (#23194202)
If the ASCII separators were supported regularly in text editors since the early days then it would remove 90% of the problems.

I prefer the | pipe character as a delimiter - easy to see, not part of common speech or titles, and enterable via keyboard. Yes, it can exist in the field but less likely.

Re:So you want to write your own CSV code (2014) by niccl on Friday May 15, 2020 @05:33PM (#23197734)
But if it _might_ turn up, you have to assume it will and deal with it, and then you're into the exact same problem as before, but with something even less 'standard' than before.

I think it's like cryptography. Why bother to roll your own when there are people who are cleverer (certainly than me. I don't know about you) who've already put a lot of effort in to this, so just use one of the well tested standard libraries and don't mess with it

Re:So you want to write your own CSV code (2014) by r00fus on Friday May 15, 2020 @07:03PM (#23198532)
I'm not recommending "rolling your own". You still use CSV libraries, but with a pipe delimiter instead.
Re:So you want to write your own CSV code (2014) by chapium on Friday May 15, 2020 @12:58PM (#23194421)
It looks a LOT like l and I.
Re:So you want to write your own CSV code (2014) by r00fus on Friday May 15, 2020 @05:30PM (#23197701)
Luckily code sees it as ASCII 124. If you're saying that for compliance, | can be converted to l/I then it's a feature, not a downside.
Re:So you want to write your own CSV code (2014) by throwaway_pdp09 on Friday May 15, 2020 @07:53AM (#23191092)
> What if a ASCII record seperator shows up

point is, being not human readable and having no keyboard key, you can reasonably expect those special separators not to.

I suppose I'd consider such special-separated files (26 to 29 from memory?) to be machine generated and machine readable only, not intended as human readable without a bit of extra software or eg. a special emacs mode

Re:So you want to write your own CSV code (2014) by roenxi on Friday May 15, 2020 @08:23AM (#23191298)
You're almost certainly describing a file format that already exists and that nobody uses because it is not human-interactable.

Anyone can edit a csv. I often have. An important feature.

The only real stumper in the CSV format is why they didn't use \ as the escape in strings like everyone else. Probably some good reason.

Re:So you want to write your own CSV code (2014) by eyelidlessness on Friday May 15, 2020 @10:32AM (#23192606)
While backslash escaping is very common, double-quote (or double-special-anything) is fairly common as well. I use it all the time in SQL.
Re:So you want to write your own CSV code (2014) by throwaway_pdp09 on Friday May 15, 2020 @09:07AM (#23191707)
Interesting. I don't think I have ever hand edited a CSV (unless you mean created from scratch, well then, perhaps).

OTOH the ability to just look at it... that I've found very valuable and agree there.

Re:So you want to write your own CSV code (2014) by sokoloff on Friday May 15, 2020 @08:07AM (#23191170)
When considering standard A that can only work with machine generated and machine readable files and standard B that can work with all those files plus human-readable/editable files, I don’t find it surprising at all that standard B wins in the market.
Re:So you want to write your own CSV code (2014) by ken on Friday May 15, 2020 @09:42AM (#23192040)
It happens. XPM/PBM/XBM lost to TIFF (and later PNG). Postscript lost to PDF. OpenOffice.org XML couldn't displace Excel's Binary File Format.

As much as I wish that beauty or usability was the primary indicator of market success, the simpler explanation that explains all of these (including CSV) is: Microsoft Office supported them.

Re:So you want to write your own CSV code (2014) by throwaway_pdp09 on Friday May 15, 2020 @09:47AM (#23192090)
I don't know about XPM/PBM/XBM lost but AIUI PDF is just a subset of postscript with compression. If you think MS's binary excel file format is all that secret, I suggest you try unzipping it - it is no more than compressed XML. I have rescued parts of corrupt excel and word files by doing just that.
Re:So you want to write your own CSV code (2014) by mark-r on Friday May 15, 2020 @11:50AM (#23193598)
MS completely redesigned their binary formats a few years ago. You're right about the current format, but the earlier one was completely undecipherable. Even the current XML has quirks in it to be compatible with the older format.
Re:So you want to write your own CSV code (2014) by mike22 on Sunday May 17, 2020 @03:07PM (#23214414)
It was reversible enough to write the Apache POI library. I maintain code that uses the HSSF streaming API portion of the library, which mainly gives you low-level access to the structures in XLS files. The OO.org folks made some very nice documentation: http://www.openoffice.org/sc/excelfileformat.pdf
Re:So you want to write your own CSV code (2014) by ken on Friday May 15, 2020 @03:32PM (#23196382)
PDF diverged from Postscript years ago.

Microsoft’s binary Excel format is not XML -- you’re thinking of its replacement. It was COM/OLE or some such.

Re:So you want to write your own CSV code (2014) by greggman3 on Friday May 15, 2020 @11:46AM (#23193526)
Binary formats win I think when the ASCII format is significantly greater in storage size and parsing speed. XPM/PBM/XBM are enormous compared to any binary image format. And remember these images used to get put on 1.4meg floppies and sent over 2400baud modems.

It's basically use the right tool for the job. For pixels that's a binary format. For mostly text, text itself works pretty well.

Re:So you want to write your own CSV code (2014) by throwaway_pdp09 on Friday May 15, 2020 @08:20AM (#23191270)
There is no standard but one for CSV files (the RFC 4180 mentioned).

The whole article pretty well rams home the fact there is no standard B because there is no standard which is adhered to.

Re:So you want to write your own CSV code (2014) by sokoloff on Friday May 15, 2020 @08:23AM (#23191300)
Humans are not bound by the computer science definition of “standard”.

But you’re correct that I was using the word “standard” imprecisely to mean something more like “file extension” rather than an IETF RFC.

Re:So you want to write your own CSV code (2014) by ISL on Friday May 15, 2020 @09:42AM (#23192049)
If someone copy-pastes a line from one CSV to a string within a field of another, the second will have record separators from the first within a single field.
Re:So you want to write your own CSV code (2014) by u801e on Friday May 15, 2020 @09:38AM (#23191993)
> CSVs are human readable and writeable and humans tend to have a comma sign on their keyboard

Do people usually compose CSV data by hand? I thought they would use a program like Excel, enter or generate their data in separate cells and then save the file as a CSV. There's no reason why a program like Excel (or any other program for that matter) couldn't use the record separator instead of a comma as a delimiter when generating or consuming such files.

Re:So you want to write your own CSV code (2014) by BiteCode_dev on Friday May 15, 2020 @10:22AM (#23192510)
Compose, rarely. Read and edit with plain text editor, it's common.
Re:So you want to write your own CSV code (2014) by mywittyname on Friday May 15, 2020 @11:50AM (#23193591)
I did it like three times already today.
Re:So you want to write your own CSV code (2014) by u801e on Friday May 15, 2020 @12:04PM (#23193750)
What editor did you use?
Re:So you want to write your own CSV code (2014) by mywittyname on Friday May 15, 2020 @03:52PM (#23196642)
vim / sublime text, depending on context.

I do a lot of troubleshooting using CLI tools like grep and wc.

Re:So you want to write your own CSV code (2014) by ganstyles on Friday May 15, 2020 @09:58AM (#23192233)
I do quite a bit for GitHub markdown tables, for making small annotations for ML, for python parsing, etc. Very common and would be annoying too open up excel every time I needed some csv.
Re:So you want to write your own CSV code (2014) by u801e on Friday May 15, 2020 @12:13PM (#23193845)
I thought markdown tables used the pipe character as the delimiter between table columns.

Also, is not necessary to use excel, and a lot of the issues brought up in the article wouldn't be the case with a different delimiter character. Editors could easily be updated to make entering that delimiter character easier to enter by mapping it to a key like tab whenever a file like that is opened.

It just seems like a problem that could be solved, but can't be due to inertia.

Re:So you want to write your own CSV code (2014) by yiyus on Friday May 15, 2020 @12:37PM (#23194140)
> I thought markdown tables used the pipe character as the delimiter between table columns.

Markdown (the original one by John Gruber) does not include any syntax for tables. Other implementations have included it, but there is no standard. As far as I know, using the pipe character to separate columns is indeed quite common, but it is not the only way.

Re:So you want to write your own CSV code (2014) by Kluny on Friday May 15, 2020 @11:41AM (#23193466)
In my experience, they'll have a database that they administrate through excel, then output it as CSV for whatever tool that needs to process it.
Re:So you want to write your own CSV code (2014) by louis_pasteur on Friday May 15, 2020 @07:30AM (#23190938)
But humans rarely use notepad these days. They use code editors like Coda or VSCode at the very least which have all kinds of advanced features. Surely, those can include support for ASCII separators?
Re:So you want to write your own CSV code (2014) by sfifs on Friday May 15, 2020 @07:59AM (#23191123)
Let's say we have a product master. Something looks fishy and we want a brand person or product supply person to check... Push a CSV and it's trivially easy for them to open in XL and check, make corrections and send.

In a business context, this happens far more often than you may expect. Sure you can build a custom platform to validate and make people connect to a form connecting to a database - but Excel is a great user interface and everyone knows excel.

A funny incident - we were struggling to build a complex workflow in KNIME where at some points we need user input. Nothing out of the box was great - tools either assume a dashboard paradigm or a data flow paradigm - nothing In between.

One of our creative folks came up with the solution of writing to a CSV and getting KNIME to open it in excel. The user would make changes and save, close excel and continue the workflow in KNIME. Even completely non technical people got it.

Re:So you want to write your own CSV code (2014) by hnick on Friday May 15, 2020 @09:20AM (#23191834)
We had to ban our non-technical users from opening CSVs in Excel because it would strip leading zeros and do other exciting things like convert large numbers to 1.23123E+20 :(

It does this without any notification or warning when you save.

Re:So you want to write your own CSV code (2014) by Tostino on Friday May 15, 2020 @01:01PM (#23194468)
Kill me...i've dealt with flat file ETL for ~9 years now, with the first 2 pretty much dedicated to it. I hate hate hate Excel for no reason other than that. It has wasted so many hours due to customers messing up their identifiers by opening up their feeds in Excel prior to sending, and accidentally transforming like above, then having to fix changing customer identifiers used throughout the system once they figure out what happened.
Re:So you want to write your own CSV code (2014) by Semaphor on Friday May 15, 2020 @10:38AM (#23192677)
Excel can only display CSV files. I had to ban myself from editing files in Excel because it’s atrocious at it. It also likes to somewhat change decimal-seperators (again, without any warning).
Re:So you want to write your own CSV code (2014) by Someone on Friday May 15, 2020 @11:18AM (#23193142)
Even technical users have trouble round tripping data to Excel.

https://genomebiology.biomedcentral.com/articles/10.1186/s13...:

“A programmatic scan of leading genomics journals reveals that approximately one-fifth of papers with supplementary Excel gene lists contain erroneous gene name conversions.”

That paper is from 2016, at least 12 years after the problem was first identified.

Re:So you want to write your own CSV code (2014) by mark-r on Friday May 15, 2020 @11:53AM (#23193625)
East coast zip codes with leading zeros are a real problem.
Re:So you want to write your own CSV code (2014) by mywittyname on Friday May 15, 2020 @11:57AM (#23193672)
That behavior is technically correct because those are all valid representations of that value, e.g., 007449938 = 7449938 = 7.449938E6. Pretty much any plain text (or binary) data format will have this same issue, unless it supports additional metadata for display purposes.

If you intended the data to be a string value, then it should have been enclosed in quotes.

Re:So you want to write your own CSV code (2014) by yiyus on Friday May 15, 2020 @12:40PM (#23194180)
The problem is that when you have a number like 123451234512345, it may get rewritten as 1.234512E+14, and then you are loosing information.
Re:So you want to write your own CSV code (2014) by hnick on Friday May 15, 2020 @09:36PM (#23199644)
Your assertion sounded incorrect so I tested it. Quoting large numbers does not preserve them on a save.

Adding a ' before it should work, but for data extracted from arbitrary systems you'll have few guarantees of the sort. Formatting the cells in Excel to display the full value instead of the truncated value also works from memory, but you won't always know ahead of time if this happened later in the file.

In our case it was often mailing identifier barcodes so any loss of precision made them entirely useless.

Re:So you want to write your own CSV code (2014) by rovolo on Friday May 15, 2020 @01:54PM (#23195151)
What if the value is an ID rather than an integer? CSV doesn't specify whether the data type is a string or a number. The schema needs to be inferred by the parser/user.
Re:So you want to write your own CSV code (2014) by recursive on Friday May 15, 2020 @02:19PM (#23195488)
> If you intended the data to be a string value, then it should have been enclosed in quotes.

That's not part of any CSV specification I've seen, including RFC4180.

Re:So you want to write your own CSV code (2014) by SifJar on Friday May 15, 2020 @08:19AM (#23191267)
but if using the ASCII separators was common, excel would support that and it seems like this workflow would be unaffected?
Re:So you want to write your own CSV code (2014) by paulryanrogers on Friday May 15, 2020 @08:31AM (#23191369)
People still need to open them in non-excel apps. And the format may predate widespread use of spreadsheet software.

Fun fact, Excel will truncate numbers beyond 15 digits unless prefixed with a single quote mark.

Re:So you want to write your own CSV code (2014) by the_angry_angel on Friday May 15, 2020 @07:40AM (#23191006)
> But humans rarely use notepad these days. They use code editors like Coda or VSCode at the very least which have all kinds of advanced features. Surely, those can include support for ASCII separators?

I do a fair amount of work with companies that do "EDI" over CSV (or worse CSV-like - think 2 CSVs jammed together with different formats, no headers, no support for escaping or quoting) and fixed width documents. I can absolutely assure you that humans do open these files in notepad far more often than I'd like.

Often one of the main reasons they don't use things like X12, ASCII separators, etc. is because a "human needs to open it at some point" was a prevailing business decision some number of years ago (think "what happens if the IT system fails? how can we still ship stuff even in a complete emergency") and now it's baked into their documented process so deeply its like shouting into the wind to alter things. Third party warehouses are the worst at this.

Re:So you want to write your own CSV code (2014) by roto on Friday May 15, 2020 @07:34AM (#23190958)
Developers are by no means the only humans that use and/or edit CSV files.
Re:So you want to write your own CSV code (2014) by beaconstudios on Friday May 15, 2020 @07:33AM (#23190950)
Developers rarely use notepad. Non technical users still have to edit csvs by hand, all over the world.
Re:So you want to write your own CSV code (2014) by C1sc0cat on Friday May 15, 2020 @09:55AM (#23192191)
The average CSV use is as an export/import from excel and most of those users don't have specialised developer editors.
Re:So you want to write your own CSV code (2014) by kube-system on Friday May 15, 2020 @10:48AM (#23192795)
I have not seen very many business analysts open an IDE. In my experience, most edit CSVs in Excel.
Re:So you want to write your own CSV code (2014) by JauntyHatAngle on Friday May 15, 2020 @07:33AM (#23190952)
I don't think your first line is at all true.

Modifying Csvs with notepad is rife. I'd wager more than using any one particular code editor.

Re:So you want to write your own CSV code (2014) by arethuza on Friday May 15, 2020 @07:38AM (#23190989)
Or they use Excel, which can introduce its own entertaining errors.

A few years back while working on something that unavoidably used large quantities of CSV data we would sternly warn people not to use Excel, but people still did it ("But I just looked at it!").

Re:So you want to write your own CSV code (2014) by snazz on Friday May 15, 2020 @08:54AM (#23191590)
The TSV (tab-separated values) format is supported by a lot of software (Google Sheets, for instance) and solves the "comma in the field" problem quite nicely. Tabs can easily be typed by a human, but they don't usually occur within a one-line string, so they don't cause the same problems as CSVs do.
Re:So you want to write your own CSV code (2014) by hulahoof on Friday May 15, 2020 @10:30AM (#23192583)
TSV, CSV, PSV etc. all share the same class of problem. The important thing is that you are quoting your fields and escaping your quotes, regardless of the symbols used.
Re:So you want to write your own CSV code (2014) by JoBrad on Friday May 15, 2020 @11:02AM (#23192961)
Importantly, tabs aren’t usually inserted into fields by Excel users. With Power BI, exporting TSV is a little easier for non-dev Excel power users, too.
Re:So you want to write your own CSV code (2014) by IggleSniggle on Friday May 15, 2020 @12:32PM (#23194081)
Nailed it. When Excel users press tab, they mean "go to the next cell." Conveniently, this is also what the format means to the computer when it encounters the tab separator. It's not foolproof, but it's much better. The human interpretation of the character matches the machine interpretation.
Re:So you want to write your own CSV code (2014) by DataWorker on Friday May 15, 2020 @02:06PM (#23195320)
“Don’t usually occur”

They do. People write code to create all sorts of bastardized abominations of “csv” or tab delim or whatever. It’s why the featured article gets reposted every few years. You can define a standard for csv files, but then Excel does it’s own thing and here we are.

Re:So you want to write your own CSV code (2014) by barbegal on Friday May 15, 2020 @07:16AM (#23190844)
I think that stems from the fact that no software ever properly supported them, they are non-printable characters so you can't see them easily. Whereas with CSV you can fire up notepad.exe and explore the data.

Given the inability to standardise on line endings "\n" "\r\n" or "\r" I don't think we can standardise on using unit or record separators

Re:So you want to write your own CSV code (2014) by tinus_hn on Friday May 15, 2020 @07:40AM (#23191008)
No, we certainly can’t expect people to use the ASCII ‘Record separator’ character as a record separator, that would be madness!
Re:So you want to write your own CSV code (2014) by mark-r on Friday May 15, 2020 @11:59AM (#23193697)
Have you ever seen the ASCII separator characters used as they were intended? I don't think I have. It's obvious the problem they were trying to solve, but it was too little too late. It doesn't help that they're control characters that aren't meant to be displayed so they're practically invisible.
Re:So you want to write your own CSV code (2014) by atrettel on Friday May 15, 2020 @11:47AM (#23193548)
CSV is human readable and writable, but that is only part of the advantage of CSV. Its simple format is great as an archival format. It's the plain text of data formats. Decades pass and you can still read it without much of an issue, though people are rightly pointing out that the format is ambiguous is nature and subject to a lot of interpretation. I've recently been compiling a lot of decades old experimental datasets, and it's honestly great when I find plain text files from the 1980s, since we can still read them! It's the more exotic data formats that came about later that are often more difficult to read properly.

The Library of Congress lists CSV files as one of its preferred formats for archival datasets: https://www.loc.gov/preservation/resources/rfs/data.html

Re:So you want to write your own CSV code (2014) by skrebbel on Friday May 15, 2020 @07:12AM (#23190824)
Bacuse csv is human readable and human writeable
Re:So you want to write your own CSV code (2014) by nojito on Friday May 15, 2020 @07:24AM (#23190895)
So is tsv, but for some reason csv is more popular despite being the worst character to use.
Re:So you want to write your own CSV code (2014) by hrktb on Friday May 15, 2020 @07:33AM (#23190953)
For a lot of people, meaningful space is a hard concept to understand.

It's made worse by the number of applications where they will see spaces reformatted willy nilly (for instance web forms eating their line breaks.

From there explaining there are spaces and tabs, and that both can look the same on screen but they are different is just asking for trouble outside of our circles.

Re:So you want to write your own CSV code (2014) by mgkimsal on Friday May 15, 2020 @09:04AM (#23191677)
> is just asking for trouble outside of our circles

it's asking for trouble in our circles too. Imagine getting a tab-sep file, opening it up in an editor and having it automatically convert to 4 spaces "because", then sending it back without checking something that doesn't look wrong.

Re:So you want to write your own CSV code (2014) by SAI_Peregrinus on Friday May 15, 2020 @11:06AM (#23193005)
Or an input/editor where the `tab` key moves your focus from the text field to the next button. Like Slack. You can't just hit `tab` in Slack when typing a code snippet for something that needs them like a Makefile.
Re:So you want to write your own CSV code (2014) by JamesSwift on Friday May 15, 2020 @10:45AM (#23192758)
What is human readable about a tab?
Re:So you want to write your own CSV code (2014) by nojito on Friday May 15, 2020 @10:48AM (#23192804)
Open up a csv in notepad vs opening up a tsv.

One actually looks like a table the other is a garbled mess.

Re:So you want to write your own CSV code (2014) by JamesSwift on Friday May 15, 2020 @11:20AM (#23193174)
I think we have different definitions of human readable then. To me, human readable means if I print it in a printer, it loses no fidelity. What you describe is what I would call machine-readable. It is able to be imported into a program. The same that a binary separator would also let it be imported.
Re:So you want to write your own CSV code (2014) by nojito on Friday May 15, 2020 @04:43PM (#23197163)
Even in your example TSV wins.
Re:So you want to write your own CSV code (2014) by steerablesafe on Friday May 15, 2020 @09:41AM (#23192035)
If those characters had a visual representation and were easily typeable and we settled on using them as separators then we would struggle escaping those special characters within records. The problem is lack of standardization, not lack of special characters for record separators.
Re:So you want to write your own CSV code (2014) by cryptonector on Friday May 15, 2020 @03:24PM (#23196292)
ASCII FS/RS/GS/US do not have a visual representation, and users do not know how to enter them. That means they're essentially for binary-like encodings -- they're not really text if users can't see or enter them easily.

But users can see the commas in CSV, and they can trivially enter them. Yeah, the result is messy.

The lesson here is that the separator control characters should have been visual and had a visual indicator on keyboard keycaps to indicate how to enter them. Because they aren't and didn't, they are essentially useless for text.

EDIT: I do happen to know how to enter these on *nix systems. The ascii(7) man page tells you how:

       034   28    1C    FS  (file separator)        134   92    5C    \  '\\'
       035   29    1D    GS  (group separator)       135   93    5D    ]
       036   30    1E    RS  (record separator)      136   94    5E    ^
       037   31    1F    US  (unit separator)        137   95    5F    _
so FS is ^\ (which you have to be careful does not cause the tty to generate SIGQUIT -- you have to ^V^\), ^], ^^, and ^_. That is, <control>\, <control>], <control>^, and <control><shift>-. On U.S. keyboards anyways.
Re:So you want to write your own CSV code (2014) by billme on Friday May 15, 2020 @07:17AM (#23190847)
Simple Google explains why:

- non printable character - no single keyboard key - intended for systems, not people - etc

Re:So you want to write your own CSV code (2014) by luckylion on Friday May 15, 2020 @07:16AM (#23190837)
Yeah, we've never had similar issues with line-endings ;)
Re:So you want to write your own CSV code (2014) by Spooky23 on Friday May 15, 2020 @10:12AM (#23192404)
I think CSV is more popular than one would think because Excel was awful back in the old days.

A lot of the crufty edge cases are artifacts of whatever people had to deal with in 1995 to get data into spreadsheets. We all got used to pushing data around with CSV because the people consuming the data needed it.

Re:So you want to write your own CSV code (2014) by hexmiles on Friday May 15, 2020 @02:37PM (#23195687)
That's funny, when i was twelve i need to store some data and i didn't really know database or csv, so i actually used the ascii control character to implement tables and record. I later discovered sqlite, soooo much simpler...
Re:So you want to write your own CSV code (2014) by carapace on Friday May 15, 2020 @12:54PM (#23194377)
This, a thousand times.

As a fun exercise write a CSV-like spec but using those ASCII chars.

    table = [row.split(unit_sep) for row in data.split(rec_sep)]
There's your ASCIISV decoder.
Re:So you want to write your own CSV code (2014) by eyelidlessness on Friday May 15, 2020 @10:35AM (#23192640)
Based on my understanding of CSV, unit separators are valid column delimiters. I doubt you could use record separators between as row delimiters though.
Re:So you want to write your own CSV code (2014) by rb808 on Friday May 15, 2020 @09:48AM (#23192107)
Wow never heard of it before. The biggest problem is Excel and text editors rule the small data world.