gocarina / gocsv

The GoCSV package aims to provide easy CSV serialization and deserialization to the golang programming language
MIT License
1.99k stars 245 forks source link

unmarshalFile how can i skip line that fail #268

Open danielzed opened 11 months ago

danielzed commented 11 months ago

help, unmarshalFile how can i skip line that fail. i suppose skip line that produce error is not supported. withErrorHandler can know which line is fail,but cannot skip that line or i can read each line and call UnmarshalBytes multiple times

want to know which way is suggested. thanks

lacombar commented 10 months ago

Having the exact same problem, my data file contains junk lines, and it would be nice to have a way to skip these...

lacombar commented 10 months ago

Found it... Expected syntax is:

import (
        "encoding/csv"
)
[...]
        errHandler := func(err *csv.ParseError) bool {
                fmt.Println(err)                                                                                                                           
                return true                                                                                                                                
        }                                                                                                                                                  

        err = gocsv.UnmarshalFileWithErrorHandler(file, errHandler, &entries)                                                                              
        if err != nil { // Load clients from file                                                                                                          
                panic(err)                                                                                                                                 
        }

However I might extend this to tweak the behavior of the error handler, ie. I might either want to ignore the error (current behavior if true is returned) or skip the row altogether.