Dealing with commas in a CSV file

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

    The simplest solution I've found is the one LibreOffice uses:

    1. Replace all literal " by
    2. Put double quotes around your string

    You can also use the one that Excel uses:

    1. Replace all literal " by ""
    2. Put double quotes around your string

    Notice other people recommended to do only step 2 above, but that doesn't work with lines where a " is followed by a ,, like in a CSV where you want to have a single column with the string hello",world, as the CSV would read:

    "hello",world"
    

    Which is interpreted as a row with two columns: hello and world"

提交回复
热议问题