-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv.go
58 lines (48 loc) · 1.2 KB
/
csv.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package tiki
import (
"encoding/csv"
"io"
"os"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
func ReadCSVFile(path string) (data [][]string, err error) {
f, err := os.Open(path)
if err != nil {
log.WithError(err).Errorf("open file %s error", path)
return nil, errors.Wrapf(err, "open file %s error", path)
}
reader := csv.NewReader(f)
for {
record, err := reader.Read()
if err != nil {
if err == io.EOF {
log.Infof("read file %s completed!", path)
break
} else {
log.WithError(err).Errorf("read file %s content error", path)
return nil, errors.Wrapf(err, "read file %s content error", path)
}
}
data = append(data, record)
}
return data, nil
}
func WriteCSVFile(path string, data [][]string) error {
f, err := os.Create(path)
if err != nil {
return errors.Wrapf(err, "cannot create file %s", path)
}
csvWriter := csv.NewWriter(f)
for _, record := range data {
if err = csvWriter.Write(record); err != nil {
log.WithError(err).Errorf("cannot write record %v to file", record)
}
}
csvWriter.Flush()
if err = csvWriter.Error(); err != nil {
log.WithError(err).Errorf("error while write data to file %s", path)
return err
}
return nil
}