登录
首页 >  Golang >  Go问答

如何为我的数据获取这个特定的形状

来源:stackoverflow

时间:2024-04-22 09:33:41 153浏览 收藏

“纵有疾风来,人生不言弃”,这句话送给正在学习Golang的朋友们,也希望在阅读本文《如何为我的数据获取这个特定的形状》后,能够真的帮助到大家。我也会在后续的文章中,陆续更新Golang相关的技术文章,有好的建议欢迎大家在评论留言,非常感谢!

问题内容

在 go 中,我使用这个函数 bars, err := custplotter.newcandlesticks(data) 从这里: https://github.com/pplcc/plotext/tree/master/custplotter

它期待数据的形状:

[{2 16435 16458 16435 16446 1} {3 16446 16458 16435.04 16455 1} .....]

但是我下面的代码正在以这种形状创建我的数据:

[[2 16435 16458 16435 16446 1] [3 16446 16458 16435.04 16455 1] .....]

这给了我这个错误消息: 无法在 custplotter.newcandlesticks 的参数中使用数据 (type [ ][ ]string) 作为 custplotter.tohlcver 类型:

[ ][ ]字符串未实现 custplotter.tohlcver(缺少 len 方法)

我认为问题在于数据形状。如何更改代码以创建所需的数据形状(使用 { } 而不是 [ ])?

//read excel file******************************************
    xlsx, err := excelize.OpenFile("/media/Snaps/test snaps.xlsm")
    if err != nil {
        fmt.Println(err)
        return
    }

    //read all rows into df
    df := xlsx.GetRows("ticker_2")

    //get only TOHLCV columns and 60 rows
    df3 := make([][]string, 60) // create slice for 60 rows
    idx := 0
    for _, row := range df[1:61] { // read 60 rows
        df3row := make([]string, 6) // create slice for 6 columns
        copy(df3row, row[28:34]) // copy desired columns to new row slice
        df3[idx] = df3row
        idx++
    }

我在 go 文献中找到的所有切片示例仅使用 [ [ ], [ ] ]


解决方案


根据 https://github.com/pplcc/plotext/blob/68ab3c6e05c34baf5af21c9f5c3341f527a110ac/examples/tohlcvexampledata.go#L42

看来您需要的是 custplotter.tohlcvs 它只是 float64 结构的一部分。

https://github.com/pplcc/plotext/blob/master/custplotter/tohlcv.go

type tohlcver interface {
    // len returns the number of time, open, high, low, close, volume tuples.
    len() int

    // tohlcv returns an time, open, high, low, close, volume tuple.
    tohlcv(int) (float64, float64, float64, float64, float64, float64)
}

// tohlcvs implements the tohlcver interface using a slice.
type tohlcvs []struct{ t, o, h, l, c, v float64 }

所以基本上你的解决方案可能类似于:

df3 := make(tohlcvs, 60) // create slice for 60 rows
idx := 0
for _, row := range df[1:61] { // read 60 rows
    df3[idx].t, err = strconv.parsefloat(row[28], 64)
    df3[idx].o, err = strconv.parsefloat(row[29], 64)
    df3[idx].h, err = strconv.parsefloat(row[30], 64)
    df3[idx].l, err = strconv.parsefloat(row[31], 64)
    df3[idx].c, err = strconv.parsefloat(row[32], 64)
    df3[idx].v, err = strconv.parsefloat(row[33], 64)
    idx++
}

或者您也可以实现 tohlcver 接口:)

type SlicesOfTOHLCV [][6]float64

func (s SlicesOfTOHLCV) Len() int {
    return len(s)
}

func (s SlicesOfTOHLCV) TOHLCV(i int) (float64, float64, float64, float64, float64) {
    return s[i][0], s[i][1], s[i][2], s[i][3], s[i][4], s[i][5]
}

mySlice := make(SlicesOfTOHLCV, 60)
i := 0
for _, row := range df[1:61] {
    mySlice[i] = [6]float64{}
    for j := 0; j < 6; j ++ {
        mySlice[i][j], err = strconv.ParseFloat(row[28+j], 64)
        if err != nil {
            panic(err)
        }
    }
    i ++
}

到这里,我们也就讲完了《如何为我的数据获取这个特定的形状》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>