Live data from Hacker News

So You Want to Write Your Own CSV code

tburette.github.io

41–50 of 127 posts

Re: So You Want to Write Your Own CSV code

#41
My most popular stackoverflow answer [1] includes a CSV writer and reader. Yeah, I'd clean it up a little if I were doing it now (return enumerator instead of array, etc). But people keep using it.

It uses regex lookaheads to deal with quoting, so it's not 100% portable. But it's only about one page.

As for the other things mentioned by the OP (BOM, encoding), those should be handled by the stream, and are not the provenance of CSV per se.

[1] http://stackoverflow.com/a/769713/4525

Re: So You Want to Write Your Own CSV code

#42
post #30

Earlier quoted context omitted.

Think it through. What if there is free text in the field? "How are you, Sally?"

You can replace all commas with a placeholder (e.g. "#COMMA#"), replace the delimiter with a comma, parse the document and then replace all placeholders in the data with ",".

That does not work, unless that first replacement magically ignores the commas that are part of field separators. If you know how to write the code that does that, your problem is solved.

Re: So You Want to Write Your Own CSV code

#43
post #27

The most retard structure I've seen in a CSV file relates to the "What if the character separating fields is not a comma?". We get "CSV" files from Klarna, an invoicing company, with the payments they've processed for us. Because we're Danish and they are Swedish, it's not really weird that they would use comma as the decimal separator. So to compensate for having used the comma, they for some reason picks ", " ( tha…

it can be irritating, but you can just as easy parse ", " to "|" or something, by simple string replacing, pre parsing..

[deleted]

Re: So You Want to Write Your Own CSV code

#44
CSV is really slow to work with, because you have to check for well-formedness, like you do with XML. And in the end, I always end up making specific concessions for the files that my customers use (which must be patched again and again) or having to take a hard stance on what can and can't be in the "CSV" files.

Re: So You Want to Write Your Own CSV code

#45

This article makes it much more complicated than it needs to be. It tries to be all things to all people. In practice you're going to have to sacrifice some functionality for the sake of usability and your own sanity. When I add a CSV import feature to a project I'm working on, I tell people "this works with MS Excel flavor of CSV." This covers most, if not all, real world cases because in my world the people who wan…

I found out that Windows Excel and Mac OS Excel use different character encodings for CSV.

Re: So You Want to Write Your Own CSV code

#46
The best tool I've found for working with CSV files is csvkit[1]. I've run into some of the issues mentioned in the article and it's handled them all gracefully. It's basically a bunch of scripts mirroring sort, grep, cut, etc. but specifically for dealing with CSV files.

[1] http://csvkit.readthedocs.org/

Re: So You Want to Write Your Own CSV code

#47
post #20

Earlier quoted context omitted.

If your data are rectangular and you care about performance, CSV is better than JSON just because it avoids repetitive key names everywhere. Then again, if your data are rectangular and you really care about performance, you would not use any of these (you might use HDF5, which has support in many programming languages and will destroy the others in terms of speed).

JSON is almost a subset of CSV, with the understanding that you have to wrap every line in [], the document in [], and every field must be quoted. (And JSON doesn't have built-in support for headings, so you need to write a little loop instead of the library building a hash for you.) So no, if you control input and output, JSON is still easier to use than CSV, and just as performant. JSON stores straight arrays just…

...and quotes inside fields must be escaped with a backslash, newlines replaced by \n, etc.

...and, the moment you declare you handle json, people will send non-string data ("that is a number, of course it isn't quoted"), attempt to include nested data, leave out the opening and closing [] (because people will grep a file with one array per line to filter a json file; that is no way robust, but people will do it, anyways)

The main advantage of json vs csv is that there is only one json, but quite a few different variant of csv. That is a huge advantage, but csv is engrained; there are many, many tools that handle csv but not json.

And the "if you control input and output" case is not the interesting or the problematic one.

Re: So You Want to Write Your Own CSV code

#48

Earlier quoted context omitted.

If Excel compatibility is the goal, one should use libraries that read and produce Excel files. CSV is bullshit, it's not good for anything except scenarios where you control both the export process, and the parser (so you know what delimiter is used and so on).

CSV files are MUCH easier to search and inspect using tools like grep and less. It's the accounting people that want's Excel, but as a developer CSV is easier and more flexible. We process csv files containing payment information, export csv as product feeds for perhaps 10 different partners. It works, it's faster than XML or JSON and is easy for non-technical people to inspect using tools like Excel. Most cvs data e…

CSV is much simpler when the records are all of one type. It gets debatable which (XML vs CSV) is simpler when you get multiple record types dumping a hierarchical data structure.

Obviously, you can make CSV files work for alternating record types, in the same way that the old mainframe files used to work with multiple record types in a file, with a type descriminator field in a know place, usually field 1, of each record. But it starts to get cluttered.

Re: So You Want to Write Your Own CSV code

#49
post #27

Earlier quoted context omitted.

it can be irritating, but you can just as easy parse ", " to "|" or something, by simple string replacing, pre parsing..

True, but in my mind picking ", " indicate to me that they don't care or don't know what they're doing. I often run into something similar with XML. I've had more than one partner call or write me saying that the elements in a file are not in the right order. Every single time they've admitted to not actually using an XML parser. Don't do things that screw up the standard tools other developers depend on.

Stuff not being in the correct order is a perfectly fine technical reason to reject an XML file if you have a DTD based workflow. It is actually quite difficult to specify that the order of elements is irrelevant (it goes with n!, so allowing six elements exactly once, but in arbitrary order in the contents of So in that case, you might have been the developer that screwed up the standard tools other developers dependan element makes that part of the DTD 720 times longer than specifying a fixed order).

So they could have easily played the ball back into your field if they had know what they did...

Post reply on HN