我正在尝试弄清楚如何在 gota library 中使用 dataframe.LoadOptions
读取 CSV 文件时指定分隔符。
package main
import (
"fmt"
"github.com/kniren/gota/dataframe"
"io/ioutil"
"strings"
)
func main() {
content, _ := ioutil.ReadFile("/path/to/csv/file.csv")
ioContent := strings.NewReader(string(content))
df := dataframe.ReadCSV(ioContent)
fmt.Println(df)
}
dataframe.ReadCSV 函数采用类型为 *dataframe.LoadOptions 的 options
变量
我检查了 dataframe.LoadOptions 结构的定义并发现了以下内容:
type LoadOption func(*loadOptions)
type loadOptions struct {
defaultType series.Type
detectTypes bool
hasHeader bool
names []string
nanValues []string
delimiter rune
types map[string]series.Type
}
我是 Go 的新手,被困在了这个问题上。我第一次遇到这样的定义:type LoadOption func(*loadOptions)
最佳答案
这个库使用 functional options .
dataframe.ReadCSV 采用 任意数量 的 dataframe.LoadOption,其中 LoadOption 是一种函数类型。查看文档以查找返回 LoadOption 值并将其传递给 ReadCSV 的内容:
dataframe.ReadCSV(ioContent,
dataframe.WithDelimiter(';'),
dataframe.HasHeader(true),
// etc.
)
关于go - 在 gota 库中使用 dataframe.ReadCSV 时指定分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49714668/