Home  >  Article  >  Backend Development  >  Why does Go\'s CSV writer add extra quotes to quoted strings containing special characters?

Why does Go\'s CSV writer add extra quotes to quoted strings containing special characters?

Susan Sarandon
Susan SarandonOriginal
2024-10-25 09:30:02186browse

Why does Go's CSV writer add extra quotes to quoted strings containing special characters?

Strange CSV Encoding of Quoted Strings in Go

This code snippet attempts to create a CSV file with two records, one with an unquoted string and the other with a quoted string containing special characters:

<code class="go">package main

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

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

    w := csv.NewWriter(f)
    var record []string
    record = append(record, "Unquoted string")
    s := "Cr@zy text with , and \ and \" etc"
    record = append(record, s)
    fmt.Println(record)
    w.Write(record)

    record = make([]string, 0)
    record = append(record, "Quoted string")
    s = fmt.Sprintf("%q", s)
    record = append(record, s)
    fmt.Println(record)
    w.Write(record)

    w.Flush()
}</code>

However, the output contains extra quotes surrounding the quoted string:

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

Explanation

This behavior is a consequence of the CSV standard. According to the specification, double quotes within a field must be escaped by double quotes. Therefore, the CSV writer automatically escapes the double quotes in the quoted string.

Solution

The CSV reader automatically unescapes the double quotes during parsing, so it is not necessary to worry about the extra quotes. The quoted string will be correctly interpreted as:

"Cr@zy text with , and \ and \" etc"

Example

Here is an improved version of the code with the unnecessary escaping removed:

package main

import (
    "encoding/csv"
    "fmt"
    "os"
)
func checkError(e error){
    if e != nil {
        panic(e)
    }
}
func writeCSV(){
    fmt.Println("Writing csv")
    f, err := os.Create("./test.csv")
    checkError(err)
    defer f.Close()

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

    record = []string{ 
      "Quoted string",
      s,
    }
    fmt.Println(record)
    w.Write(record)
    w.Flush()
}
func readCSV(){
    fmt.Println("Reading csv")
    file, err := os.Open("./test.csv")
    defer file.Close();
    cr := csv.NewReader(file)
    records, err := cr.ReadAll()
    checkError(err)
    for _, record := range records {
        fmt.Println(record)
    }
}
func main() {
   writeCSV()
   readCSV()
}

Output

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

The above is the detailed content of Why does Go\'s CSV writer add extra quotes to quoted strings containing special characters?. 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