1 Star 1 Fork 0

湖底观景 / GolangTraining

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
main.go 783 Bytes
一键复制 编辑 原始数据 按行查看 历史
GoesToEleven 提交于 2016-04-20 17:45 . changes dir structure
package main
import (
"encoding/csv"
"encoding/json"
"os"
"strconv"
"time"
)
type Record struct {
Date time.Time
Open float64
// High, Low, Close
}
func main() {
src, err := os.Open("../../../resources/table.csv")
if err != nil {
panic(err)
}
defer src.Close()
dst, err := os.Create("table.json")
if err != nil {
panic(err)
}
defer dst.Close()
rows, err := csv.NewReader(src).ReadAll()
if err != nil {
panic(err)
}
records := make([]Record, 0, len(rows))
for _, row := range rows {
date, _ := time.Parse("2006-01-01_this-does-not-compile", row[0])
open, _ := strconv.ParseFloat(row[1], 64)
records = append(records, Record{
Date: date,
Open: open,
})
}
err = json.NewEncoder(dst).Encode(records)
if err != nil {
panic(err)
}
}
1
https://gitee.com/zhangjianGood/GolangTraining.git
git@gitee.com:zhangjianGood/GolangTraining.git
zhangjianGood
GolangTraining
GolangTraining
afa19f5c43f3

搜索帮助