【发布时间】:2018-04-09 08:55:16
【问题描述】:
我正在尝试解析最后一列中包含 JSON 对象的 CSV 文件。
以下是输入 CSV 文件中包含两行的示例:
'id','value','createddate','attributes'
524256,CAFE,2018-04-06 16:41:01,{"Att1Numeric": 6, "Att2String": "abc"}
524257,BEBE,2018-04-06 17:00:00,{}
我尝试使用 csv 包中的解析器:
func processFileAsCSV(f *multipart.Part) (int, error) {
reader := csv.NewReader(f)
reader.LazyQuotes = true
reader.Comma = ','
lineCount := 0
for {
line, err := reader.Read()
if err == io.EOF {
break
} else if err != nil {
fmt.Println("Error:", err)
return 0, err
}
if lineCount%100000 == 0 {
fmt.Println(lineCount)
}
lineCount++
fmt.Println(lineCount, line)
processLine(line) // do something with the line
}
fmt.Println("done!", lineCount)
return lineCount, nil
}
但我得到一个错误:
错误:第 2 行,第 0 列:行中的字段数错误,
可能是因为解析器忽略了以{ 开头的 JSON 范围。
我应该编写自己的 CSV 解析器,还是有可以处理这个的库?
【问题讨论】:
-
请发布您尝试过的内容。