Golang解析CSV的封闭规则

huangapple go评论85阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2014年7月31日 22:59:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/25062281.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定