Strange CSV result for quoted strings in go encoding/csv
csv, escaping, go, string
Solution
It's part of the standard for storing data as CSV. Double quote characters need to be escaped for parsing reasons.
A (double) quote character in a field must be represented by two (double) quote characters.
From: http://en.wikipedia.org/wiki/Comma-separated_values
You don't really have to worry because the CSV reader un-escapes the double quote.
Example:
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",
fmt.Sprintf("%q",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"]
Here's the code for the write function. func (w *Writer) Write(record []string) (err error)
Problem
I have this little bit of code that kept me busy the whole weekend. ``` 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() } ``` When run it prints out: ``` [Unquoted string Cr@zy text with , and \ and " etc] [Quoted string "Cr@zy text with , and \\ and \" etc"] ``` The second, quoted text is exactly what I would wish to see in the CSV, but instead I get this: ``` Unquoted string,"Cr@zy text with , and \ and "" etc" Quoted string,"""Cr@zy text with , and \\ and \"" etc""" ``` Where do those extra quotes come from and how do I avoid them? I have tried a number of things, including using strings.Quote and some such but I can't seem to find a perfect solution. Help, please?