Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encoding/csv: add field ForceQuotes to Writer #12755

Closed
stemar94 opened this issue Sep 25, 2015 · 6 comments
Closed

encoding/csv: add field ForceQuotes to Writer #12755

stemar94 opened this issue Sep 25, 2015 · 6 comments

Comments

@stemar94
Copy link

With a boolean field ForceQuotes in the csv.Writer one could force quotation of fields.

@gopherbot
Copy link

CL https://golang.org/cl/15013 mentions this issue.

@bradfitz
Copy link
Contributor

Why?

@stemar94
Copy link
Author

I had some compability problems, when forced to escape fields not needed to be escaped by current implementation.

Each field may or may not be enclosed in double quotes

Even if there is no actual specification I guess the RFC is status quo.

And if you would wrap something around the writer as

func WriteWrapper(w *csv.Writer, record []string) error {
    for i, field := range record {
         record[i] = `"` + field + `"`
    }
    return w.Write(record)
}

you would end up with """field1""","""field2"""

@stemar94 stemar94 changed the title encoding/csv: add field Quote to Writer encoding/csv: add field ForceQuote to Writer Sep 27, 2015
@stemar94 stemar94 changed the title encoding/csv: add field ForceQuote to Writer encoding/csv: add field ForceQuotes to Writer Sep 27, 2015
@rsc
Copy link
Contributor

rsc commented Oct 23, 2015

This package can't be all things to all people.

Unless the compatibility problems you alluded to are widespread, I'd prefer not to add more complexity to this package. I believe this package as it stands today matches the CSV produced by Microsoft Excel and Google Drive. That should be good enough.

If you really just want to print fully-quoted CSV, you don't even need this package:

func printCSV(w io.Writer, data [][]string) {
    for _, row := range data {
        sep := ""
        for _, cell := range row {
            fmt.Printf(`%s"%s"`, sep, strings.Replace(cell, `"`, `""`, -1))
            sep = ","
        }
        fmt.Printf("\n")
    }
}

@rsc rsc closed this as completed Oct 23, 2015
@gopherbot
Copy link

CL https://golang.org/cl/23401 mentions this issue.

gopherbot pushed a commit that referenced this issue May 25, 2016
The intent of this comment is to reduce the number of issues opened
against the package to add support for new kinds of CSV formats, such as
issues #3150, #8458, #12372, #12755.

Change-Id: I452c0b748e4ca9ebde3e6cea188bf7774372148e
Reviewed-on: https://go-review.googlesource.com/23401
Reviewed-by: Andrew Gerrand <adg@golang.org>
@u007
Copy link

u007 commented Sep 8, 2016

what about
\n and \r\n
?

@golang golang locked and limited conversation to collaborators Sep 8, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants