Live data from Hacker News

So You Want to Write Your Own CSV code

tburette.github.io

61–70 of 127 posts

Re: So You Want to Write Your Own CSV code

#63
We actually use CSV-reading as an incidental part of a hiring exercise. We provide a really simple homemade CSV parser as part of a PHP project, with a "could you find and fix bugs in this?" instruction. The way to get full marks is to rip out the parser and replace it with the appropriate standard library function.

Re: So You Want to Write Your Own CSV code

#64

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…

In General Swedish csv files are separated with ;

I have an function that I usually use in projects that counts , and ; on each line to determine which one is most likly beeing used in the file.

The most annoying thing I have found in csv files is the escape sign I would like it to be \" but very often I see """ as the escape for "

Re: So You Want to Write Your Own CSV code

#65

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…

In General Swedish csv files are separated with ; I have an function that I usually use in projects that counts , and ; on each line to determine which one is most likly beeing used in the file. The most annoying thing I have found in csv files is the escape sign I would like it to be \" but very often I see """ as the escape for "

I believe that the Swedish Klarna files uses ; but the Danish ones uses the comma + space. That only adds to the stupidity though, why not have just one format?

Re: So You Want to Write Your Own CSV code

#66
One of my first open source projects was a JDBC driver that read CSV files. It started simply enough but once you started adding in support for all the quirks things became really complicated really quickly. Just check out all the "options" for the driver that have been added by the community over the last 14ish years http://csvjdbc.sourceforge.net/doc.html

Re: So You Want to Write Your Own CSV code

#67
post #61

CSV works for simple cases. It is trivial to parse, you shouldn't even need a library. It there are many "what ifs" like in the posted article. You probably need another format like JSON (preferably) or XML.

Yet in the real world you don't often have such luxury. Often times that inconsistent CSV file that you have to parse is not of your own creation. It comes from some other data source, or perhaps you have multiple data sources spitting out their own variants. You just need to get the job done, and splitting on ',' won't work.

Re: So You Want to Write Your Own CSV code

#68
post #57

Earlier quoted context omitted.

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.

I'm mostly using Python and trying to learn Go, both have great csv parsers and I used the one in PHP. Given the size and features present in .Net I was really suprised to not find a csv parser.

Re: So You Want to Write Your Own CSV code

#70
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…

> absolutely ignore people who's argument is along the lines of "you are not smart enough to implement this standard. let someone else do it.”. fuck everything about that statement, and it’s false sense of superiority.

In general it's not about being smart enough (although for some complicated standards maybe it's true), but rather biting off more than you realize. Everything sounds simple before you find the edge case implementation issues and have to rework and rethink a bunch of hard issues that a dozen people have already thought through. Doing it yourself is on the table, but rarely the most efficient decision.

Post reply on HN