Dealing with commas in a CSV file

后端 未结 27 2818
傲寒
傲寒 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

    First, let's ask ourselves, "Why do we feel the need to handle commas differently for CSV files?"

    For me, the answer is, "Because when I export data into a CSV file, the commas in a field disappear and my field gets separated into multiple fields where the commas appear in the original data." (That it because the comma is the CSV field separator character.)

    Depending on your situation, semi colons may also be used as CSV field separators.

    Given my requirements, I can use a character, e.g., single low-9 quotation mark, that looks like a comma.

    So, here's how you can do it in Go:

    // Replace special CSV characters with single low-9 quotation mark
    func Scrub(a interface{}) string {
        s := fmt.Sprint(a)
        s = strings.Replace(s, ",", "‚", -1)
        s = strings.Replace(s, ";", "‚", -1)
        return s
    }
    

    The second comma looking character in the Replace function is decimal 8218.

    Be aware that if you have clients that may have ascii-only text readers that this decima 8218 character will not look like a comma. If this is your case, then I'd recommend surrounding the field with the comma (or semicolon) with double quotes per RFC 4128: https://tools.ietf.org/html/rfc4180

提交回复
热议问题