Home >Backend Development >Golang >Why does Go\'s CSV writer add extra quotes to quoted strings containing special characters?
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"""
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.
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"
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 := "Cr@zy text with , and \ and \" etc"
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()
}
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!