我想从文件中读取数据。但文件在文件末尾有不同的字段。我也想提取这些线,当遇到.我怎么能这样做。csvErrFieldCount
package main
import (
"encoding/csv"
"fmt"
"io"
"log"
"strings"
"errors"
)
func main() {
in := `first_name,last_name,username
"Rob","Pike",rob
Ken,Thompson,ken
"Robert","Griesemer"
`
r := csv.NewReader(strings.NewReader(in))
for {
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
if errors.Is(err, csv.ErrFieldCount) {
// fmt.Println("#", differentRow")
log.Fatal("wrong fields")
//TODO I want to pick it up here
}else{
log.Fatal(err)
}
}
fmt.Println(record)
}
}
我的预期输出:
[first_name last_name username]
[Rob Pike rob]
[Ken Thompson ken]
# "Robert","Griesemer". <---- my expect line ...
2009/11/10 23:00:00 wrong fields
料青山看我应如是
相关分类