-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv.go
115 lines (102 loc) · 2.37 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package gnfmt
import (
"bytes"
"encoding/csv"
"os"
"runtime"
"strings"
"unicode"
"unicode/utf8"
)
// ReadHeaderCSV only reads the first line of a CSV/TSV input. It takes a path
// to a CSV/TSV-encoded file and a separator character. It returns back a map
// with name of a field, and its index. It also returns an error, if the header
// could not be read.
func ReadHeaderCSV(path string, sep rune) (map[string]int, error) {
res := make(map[string]int)
f, err := os.Open(path)
if err != nil {
return res, err
}
defer f.Close()
r := csv.NewReader(f)
r.Comma = sep
// skip header
header, err := r.Read()
if err != nil {
return nil, err
}
for i, v := range header {
res[v] = i
}
return res, nil
}
// ToCSV takes a slice of strings and converts them to a CSV/TSV row with '"'
// as a field quote. On Windows machine the row ends with '\r\n', in all other
// cases with '\n'
func ToCSV(record []string, sep rune) string {
var b bytes.Buffer
useCRLF := runtime.GOOS == "windows"
for i, field := range record {
if i > 0 {
b.WriteRune(sep)
}
if !fieldNeedsQuotes(field, sep) {
b.WriteString(field)
continue
}
b.WriteByte('"')
for len(field) > 0 {
// Search for special characters.
ii := strings.IndexAny(field, "\"\r\n")
if ii < 0 {
ii = len(field)
}
// Copy verbatim everything before the special character.
b.WriteString(field[:ii])
field = field[ii:]
// Encode the special character.
if len(field) > 0 {
switch field[0] {
case '"':
b.WriteString(`""`)
case '\r':
if !useCRLF {
b.WriteByte('\r')
}
case '\n':
if useCRLF {
b.WriteString("\r\n")
} else {
b.WriteByte('\n')
}
}
field = field[1:]
}
}
b.WriteByte('"')
}
return b.String()
}
// NormRowSize takes a row with less or more fields than required and
// either truncates it, or expands with empty fields to fit to the
// fieldsNum size.
func NormRowSize(row []string, fieldsNum int) []string {
if len(row) > fieldsNum {
return row[:fieldsNum]
}
for range fieldsNum - len(row) {
row = append(row, "")
}
return row
}
func fieldNeedsQuotes(field string, sep rune) bool {
if field == "" {
return false
}
if field == `\.` || strings.ContainsRune(field, sep) || strings.ContainsAny(field, "\"\r\n") {
return true
}
r1, _ := utf8.DecodeRuneInString(field)
return unicode.IsSpace(r1)
}