Live data from Hacker News

So You Want to Write Your Own CSV code

tburette.github.io

51–60 of 127 posts

Re: So You Want to Write Your Own CSV code

#51
post #6

Why are people using CSV when better (and less fuzzily defined) solutions exist, such as JSON?

The idea that JSON is the substitute made me chuckle. JSON is more verbose to boot. CSV is a poor format but JSON is not panacea, actually personally I'd never use it for anything that's not web (browser) related.

Re: So You Want to Write Your Own CSV code

#52
CSVs were simpler back in the 80s, when there were a few products (e.g. - Lotus 123, xBASE) that all wrote RFC 4180 compliant text (and I'm pretty sure there was no RFC 4180 yet)

No alternate delimiters, no backslashes.

Now I have to put up with offshore staff trying to use apostrophes (') instead of quotes (") :-(

Barring alternate delimiters, and disallowing newlines* in fields, I can write the parser for 4180 in about 30 lines of perl, reading a char at a time and flipping between about 4 states. (avoids getting root access and days of paperwork to install from CPAN)

* disallowing newlines in the data is admittedly a big restriction, but it works for many use-case/applications, and allows the caller to pull in a line before calling the parse function.

For Java, the "Ostermiller" library is pretty good for CSV handling, and has a few options for dealing with freaky variants.

Re: So You Want to Write Your Own CSV code

#53
We switched from a CSV based delivery to Apache Avro files. These are binary files which have the record schema embedded in the file header. We're pretty happy with this solution for the time being and it seems to be an awesome alternative to CSV. I wonder if anyone else is doing something similar? Good article but I'd appreciate if the author gave some alternatives.

Re: So You Want to Write Your Own CSV code

#54
post #49

Earlier quoted context omitted.

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 depen…

That is much more complex than the issues we've seen. My issue is with say a with the subelements and , it should never make any difference if email or name is first or second.

Re: So You Want to Write Your Own CSV code

#55
post #36

CSV are a headache. Like the article says, RFC4180 doesn't necessarily represent the real world. However sometimes you just have to reject things that aren't spec. Not too long ago I was struggling with one of these CSV issues and received some good advice from Hans Passant [1] on a Stack Overflow question pertaining to my problem (emphasis mine): "It is pretty important that you don't try to fix it. That will make y…

On the other hand, the ability to handle all kinds of input can be a chief selling point of your product.

In my current job, the most common "invalid" CSV format we get is .xlsx files.

So I wrote an .xlsx parser (way, way faster than Apache POI).

Another interesting hiccup to consider is CSV inside individual fields - i.e. recursive CSV. There are various ways to handle this, but in my company's line of business the usual route is to duplicate that line once per CSV element found in the field.

Likely the next invalid format we'll have to parse is PDFs containing tables...

Re: So You Want to Write Your Own CSV code

#56
post #25

as the article mentions, CSV is not well defined. libraries are.. well, different. you'd spend as much time becoming familiar with one as you would writing a basic parser. commas don't delimit field entries? CSV -> comma separated values. new lines inside a field? i've never written a parser that would be foiled by this. could be an issue if you use a built-in tokeniser (e.g strtok, etc.). be aware. variable number o…

On the one hand, I hear you. On the other hand...

Too many "enterprise" coworkers who don't know how to write a finite state machine. They do need to use a library.

Re: So You Want to Write Your Own CSV code

#57
post #2

So, which library? CSV is a mess.

I would say use the parser included in your programming environment, but I spend way to long looking of the non existing CSV parser in .Net.

Not a single language I use on a routine basis includes a CSV parser in its standard library.

Re: So You Want to Write Your Own CSV code

#58
post #36

CSV are a headache. Like the article says, RFC4180 doesn't necessarily represent the real world. However sometimes you just have to reject things that aren't spec. Not too long ago I was struggling with one of these CSV issues and received some good advice from Hans Passant [1] on a Stack Overflow question pertaining to my problem (emphasis mine): "It is pretty important that you don't try to fix it. That will make y…

I've never really got my head round RFCs, but 4180 is only informational, not a standard. I have used exactly your argument before though, and will again. Have also been on the other side and needed to convert horribly inconsistent data to fit it.

Re: So You Want to Write Your Own CSV code

#59
> What if the character separating fields is not a comma?

> Not kidding.

We'd ll be better off really, but that ship has sailed. Using CSV for data which is only ever read by a machine is a dumb decision. Use the RS (record separator) character and many of these ambiguities disappear.

Of course, like I said, that ship has sailed. If you want your data to be read nicely by other programs you're probably stuck with CSV, TSV, or something similar.

Re: So You Want to Write Your Own CSV code

#60
"CSV is not a well defined file-format. The RFC4180 does not represent reality. It seems as every program handles CSV in subtly different ways. Please do not inflict another one onto this world. Use a solid library."

I can't but disagree when I read stuff like this. Why shouldn't I release a library if I think it's good enough for the community? Even the powerful and versatile Ruby library for CSV parsing started as a gem from a person who didn't give a s... about advise like "do not inflict another one into this world".

Post reply on HN