Home  >  Article  >  Backend Development  >  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`?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-26 20:17:29700browse

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.

<code class="go">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()
}</code>

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

<code class="go">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)
}</code>

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.

The above is the detailed content of Why does my Go code produce extra quotes when writing quoted strings to a CSV file using `encoding/csv`?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn