"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Why does my Go code produce extra quotes when writing quoted strings to a CSV file using `encoding/csv`?

Why does my Go code produce extra quotes when writing quoted strings to a CSV file using `encoding/csv`?

Published on 2024-11-12
Browse:630

Why does my Go code produce extra quotes when writing quoted strings to a CSV file using `encoding/csv`?

Strange CSV Results for Quoted Strings Using Go's Encoding/CSV

In this code snippet, the goal is to write data into a CSV file, ensuring that quoted strings within the data are properly escaped. However, the resulting CSV contains extra quotes, leading to inconsistencies.

package main

import (
    "encoding/csv"
    "fmt"
    "log"
    "os"
)

func main() {
    f, err := os.Create("test.csv")
    if err != nil {
        log.Fatal(err)
    }
    defer f.Close()

    w := csv.NewWriter(f)
    record := []string{"Unquoted string", "Cr@zy text with , and \\ and \" etc"}
    w.Write(record)

    record = []string{"Quoted string", fmt.Sprintf("%q", "Cr@zy text with , and \\ and \" etc")}
    w.Write(record)

    w.Flush()
}

The expected output for the quoted string is:

[Unquoted string Cr@zy text with , and \ and " etc]
[Quoted string "Cr@zy text with , and \\ and \" etc"]

However, the actual output contains extra quotes:

Unquoted string,"Cr@zy text with , and \ and "" etc"
Quoted string,"""Cr@zy text with , and \\ and \"" etc"""

Understanding the Extra Quotes

The extra quotes in the quoted string are a result of following the CSV standard, which requires double quotes to be escaped as two double quotes. This is necessary to distinguish between actual double quotes within the data and those used for record delimitation.

Solution

The code does not need to worry about escaping double quotes because the CSV reader unescapes them automatically. Therefore, the solution is to remove the extra double quotes when writing the quoted string.

Modified Code

for _, record := range [][]string{
    {"Unquoted string", "Cr@zy text with , and \\ and \" etc"},
    {"Quoted string", "Cr@zy text with , and \\ and \" etc"},
} {
    record[1] = fmt.Sprintf("%q", record[1][1:len(record[1])-1])
    w.Write(record)
}

Updated Output

Unquoted string,Cr@zy text with , and \ and " etc
Quoted string,"Cr@zy text with , and \\ and \" etc"

With this change, the quoted string is now properly escaped and the extra quotes are removed.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3