英文:
Golang enclosure rule for csv parsing
问题
我有以下问题。我需要读取以'|'分隔的csv文件,其中一些字段包含大型的json字符串,其中一些字符串中包含'|'。
字段被单引号括起来。
我该如何在GOLANG中指定字段被单引号括起来。
目前的设置如下:
fx := csv.NewReader(fz)
fx.Comma = '|'
fx.LazyQuotes = true
fx.FieldsPerRecord = 16
我在GODOCS选项中没有看到任何关于封闭规则的内容。
有人有解决这个问题的方法吗?
英文:
I have the following problem. I have to read csv files that are delimited by '|', and some fields have large json strings in which some contain '|'.
The fields are enclosed in single-quotes.
How can I specify in GOLANG that the fields are enclosed in single quotes.
This is how it is setup right now:
fx := csv.NewReader(fz)
fx.Comma = '|'
fx.LazyQuotes = true
fx.FieldsPerRecord = 16
I don't see anything in GODOCS options for enclosure rules.
Does anyone have a workaround for this ?
答案1
得分: 5
你可以复制$GOROOT/src/pkg/encoding/csv/reader.go
文件,然后修改line 274,289,297和303行,将'"'
替换为'\''
,然后你就可以使用了。
英文:
You can't, however, luckily Go is written in, well, Go.
You could copy $GOROOT/src/pkg/encoding/csv/reader.go
, then modify line 274, 289, 297 and 303 to use '\''
instead of '"'
and you're good to go.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论