Dealing with commas in a CSV file

后端 未结 27 2817
傲寒
傲寒 2020-11-21 06:53

I am looking for suggestions on how to handle a csv file that is being created, then uploaded by our customers, and that may have a comma in a value, like a company name.

27条回答
  •  野性不改
    2020-11-21 07:17

    As this is about general practices let's start from rules of the thumb:

    1. Don't use CSV, use XML with a library to read & write the xml file instead.

    2. If you must use CSV. Do it properly and use a free library to parse and store the CSV files.

    To justify 1), most CSV parsers aren't encoding aware so if you aren't dealing with US-ASCII you are asking for troubles. For example excel 2002 is storing the CSV in local encoding without any note about the encoding. The CSV standard isn't widely adopted :(. On the other hand xml standard is well adopted and it handles encodings pretty well.

    To justify 2), There is tons of csv parsers around for almost all language so there is no need to reinvent the wheel even if the solutions looks pretty simple.

    To name few:

    • for python use build in csv module

    • for perl check CPAN and Text::CSV

    • for php use build in fgetcsv/fputcsv functions

    • for java check SuperCVS library

    Really there is no need to implement this by hand if you aren't going to parse it on embedded device.

提交回复
热议问题