dongqu3623 2018-10-24 20:50
浏览 134
已采纳

如何将Gota数据帧写入CSV?

I have found many many code examples of writing to a CSV by passing in a [][]string. (like the following):

package main

import (
    "os"
    "log"
    "encoding/csv"
)

var data = [][]string{
    {"Row 1", "30"}, 
    {"Row 2", "60"},
    {"Row 3", "90"}}

func main() {
    file, err := os.Create("tutorials_technology.csv")
    if err != nil {
        log.Fatal(err)
    }
    defer file.Close()

    w := csv.NewWriter(file) 

    for _, value := range data {
        if err := w.Write(value); err != nil {
            log.Fatalln("Error writing record to csv: ", err)
        }
    }
    w.Flush()
}

However, I haven't found any code examples that show how to use the gota dataframe.WriteCSV() function to write to a CSV. In the gota dataframe documentation, there isn't an example for writing to a csv, but there is an example for reading from a csv.

The dataframe function WriteCSV() requires an input of the io.Writer{} interface. I wasn't sure how to satsify that.

The following didn't work

writer := csv.NewWriter(f)

df.WriteCSV(writer) // TODO This writer needs to be a []byte writer

I've been working on this for quite a while. Does anyone have any clues?

I have looked into turning my gota dataframe into a [][]string type, but that's a little inconvenient because I put my data into a gota dataframe with the package's LoadStructs() function and I had read in some CSV in a semi-custom way before putting them into structs.

So I could write a function to turn my structs into a [][]string format, but I feel like that is pretty tedious and I'm sure there has got to be a better way. In fact, I'm sure there is because the dataframe type has the WriteCSV() method but I just haven't figured out how to use it.

Here are my structs

type CsvLine struct {
    Index  int
    Date   string
    Symbol string
    Open   float64
    High   float64
    Low    float64
    Close  float64
    // Volume float64
    // Market_Cap float64
}

type File struct {
    Rows []CsvLine
}

Disclaimer: I am a little bit of a golang newbie. I've only been using Go for a couple months, and this is the first time I've tried to write to a file. I haven't interacted much with the io.Writer interface, but I hear that it's very useful.

And yes, I frequently look at the Golang.org blog and I've read "Effective Go" and I keep referencing it.

  • 写回答

1条回答 默认 最新

  • dtqie02844 2018-10-24 20:50
    关注

    So it turns out I misunderstood the io.Writer interface and I didn't understand what the os.Create() function returns.

    It turns out the code is even simpler and easier than I thought it would be.

    Here is the working code example:

    df := dataframe.LoadStructs(file.Rows)
    
    f, err := os.Create(outFileName)
    if err != nil {
        log.Fatal(err)
    }
    
    df.WriteCSV(f)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢