Dealing with commas in a CSV file

后端 未结 27 2799
傲寒
傲寒 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:26

    You can put double quotes around the fields. I don't like this approach, as it adds another special character (the double quote). Just define an escape character (usually backslash) and use it wherever you need to escape something:

    data,more data,more data\, even,yet more

    You don't have to try to match quotes, and you have fewer exceptions to parse. This simplifies your code, too.

提交回复
热议问题