28

YOU CANNOT parse CSV by just splitting the string by commas.

YOU CANNOT generate CSV by just outputting the raw values separated by commas.

CSV is not the magical parseless data format. You need to read fields in quotations, and newlines inside of fields shouldn't prematurely end the row.

Do it fucking right holy shit.

Comments
  • 5
    There's no universal right way to do csv, that's just part of the experience
  • 2
    In Germany - for excel compatibility - CSVs have a semicolon as an delimiter.

    As a comma is a separator for digits - e.g. 3,14

    CSV parsing requires knowledge of the delimiter (is it a comma or not xD), quotation and newline separator.

    Otherwise... It's garbage
  • 5
    @jespersh Why? It’s basically the easiest parser you will ever write.
  • 3
    @dotnethostage forbidding newlines inside of fields is a ridiculous constraint for a data file, though.
  • 2
    @AlgoRythm why would you use csv for such data though
  • 1
    And here I am properly escaping my values to use in CSV generation.

    Who knew it was so easy to just use something out of the box. /s
  • 0
    All you have to do is escape and wrap all data.

    Yes type checks get fucked in the ass, but fuck types when you have to deal with Excel as the software opening this shit. I'd rather a csv that opens and the data doesn't jump around cells then worry about if it's a string, if it's a number with a comma or a leading 0 🤷‍♂️

    Everything just became a string!
  • 0
    This is why I have CSV.
  • 2
    Looking at the regexes here and other stuff....

    Most languages have an CSV im -/ export function for that reason.

    And regexes are pretty much the worst approach to a tokenized string.

    Just have a clear spec, rest is easy.
  • 2
    JSON.
  • 1
    @dotnethostage why not.... Just saying it like this?

    ;) I dunno, but when someone tries to impress me with regexes, I want to make him a sandwich, puff his cheeks and treat him like a lil preschooler xD

    Or did I get the impression part wrong? XD
  • 0
    @dotnethostage I think you misunderstood me.

    I was a bit baffled by you trying to find a matching regex as a formal way to prove that your statement is correct. :)

    I like regular expressions and I'm not feeling intimated at all.

    It's just that the statement "It's a simple non-general recursive grammar" would have been sufficient, the extra effort of an regex seemed like you tried to gain some extra points like in a contest.
  • 0
    @dotnethostage then apologize my rudeness and take this cookie as an peace offering :)
  • 1
    Just dropped by to say

    I don't know about you guys, but I am intimidated by that regex, personally.
  • 1
    @hardCoding I tell every dev the same thing that says that.

    Regex will change your life. Start small, and pick one flavor of it(python, ruby, sed). Once you feel comfortable in one, the rest kinda fall into place.

    After that it's easy to switch between pcre, posix, etc.
Add Comment