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: multiple-quoted in a field, Unable to correctly identify #66502

Closed
ccaiy9 opened this issue Mar 24, 2024 · 3 comments
Closed

encoding/csv: multiple-quoted in a field, Unable to correctly identify #66502

ccaiy9 opened this issue Mar 24, 2024 · 3 comments

Comments

@ccaiy9
Copy link

ccaiy9 commented Mar 24, 2024

Go version

GOVERSION="go1.19.13", GOARCH="amd64"

Output of go env in your module/workspace:

GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/root/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/root/go"
GOPRIVATE=""
...
GOVERSION="go1.19.13"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"

What did you do?

I want to read data from csv file and insert it into mysql, mysql table struct is:

CREATE TABLE `test` (
  `"name'"` varchar(32) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3

1. my csv file:

""name'""
"NULL"
"""name'"""

2. my code demo:

r := csv.NewReader(os.Stdin)
r.LazyQuotes = true
lst, err := r.Read()
logger.Warningf(context.Background(), "lst: %+v", lst)

the lst result is :
["name'"
"NULL]

What did you see happen?

My code prints the result:
["name'"
"NULL]

What did you expect to see?

My expected result should be: ["name'"]
So, is this a BUG or is there some wrongs in my code?
What should I do to get the expected result?

@seankhliao
Copy link
Member

that doesn't look like a valid quoting of values for csv

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Mar 24, 2024
@ccaiy9
Copy link
Author

ccaiy9 commented Mar 24, 2024

that doesn't look like a valid quoting of values for csv

Can you explain why this is not a valid CSV value?

""name'""
"NULL"
"""name'"""

read from csv.NewReader should be?

"name'"
NULL
""name'""

@seankhliao
Copy link
Member

LazyQuotes = true, so quotes in fields don't have to be doubled:
It's parsed as:

  • ": start of quoted field
  • ": free floating quote
  • name': text
  • "": doubled quote
  • newline: text
  • ": free floating quote
  • NULL: text
  • " end of quoted field

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants