-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv.go
56 lines (47 loc) · 1.01 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
package main
import (
"encoding/csv"
"io"
"strconv"
"strings"
"time"
)
type CsvTransaction struct {
TransctionId string
Date time.Time
Amount int64
Payee string
Category string
Memo string
Address string
}
func ReadFromCSV(contents string) ([]CsvTransaction, error) {
transactions := []CsvTransaction{}
r := csv.NewReader(strings.NewReader(contents))
r.Read() //skip first line
for {
transaction := CsvTransaction{}
line, err := r.Read()
if err != nil {
if err == io.EOF {
break
} else {
panic(err)
}
}
transaction.TransctionId = line[0]
date, err := time.Parse("02/01/2006", line[1])
check(err)
transaction.Date = date
transaction.Memo = line[11]
transaction.Category = line[6]
transaction.Payee = line[4]
transaction.Address = line[12]
amount, err := strconv.ParseFloat(line[7], 64)
check(err)
amount *= 100
transaction.Amount = int64(amount)
transactions = append(transactions, transaction)
}
return transactions, nil
}