将结构写入csv文件 [英] Write struct to csv file

查看:119
本文介绍了将结构写入csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将结构转储到提供的csv文件中的惯用golang方法是什么?

  func decode_and_csv(my_response * http.Response,my_struct接口{})

为什么界面{}? - 从JSON中读取数据,可能会返回一些不同的结构,因此试图编写一个通用的函数。



我的类型示例:

  type Location [] struct {
名称字符串`json:名称``
区域字符串`json:Region `
类型字符串`json:类型``
}


解决方案

如果你使用了一个具体的类型,它会容易得多。你可能会想要使用 encoding / csv 包,这里是一个相关的例子; https://golang.org/pkg/encoding/csv/#example_Writer



正如您所看到的, Write 方法期望 []字符串所以为了生成这个,你必须要么1)提供一个辅助方法或2)反映 my_struct 。就个人而言,我更喜欢第一种方法,但它取决于您的需求。如果你想要走第二条路线,你可以得到结构上的所有字段,并将它们用作列标题,然后迭代获取每个字段值的字段,在> append 中使用<该循环将它们添加到 []字符串中,然后将它传递给循环外的 Write >。 p>

对于第一个选项,我会定义一个 ToSlice 或者每种类型的东西,然后我会让一个接口调用它 CsvAble ,它需要 ToSlice 方法。改变你的方法中的类型 my_struct CsvAble 而不是使用空接口,然后你可以在<$上调用 ToSlice c $ c> my_struct 并将返回值传递给 Write 。你也可以返回列标题(这意味着你会得到一个 [] []字符串,并且需要迭代传递每个 [] string 转换为 Write ),或者您可能需要其他方法来满足 GetHeaders 返回一个 []字符串这是列标题。如果是这样的话,你的代码看起来就像这样:

  w:= csv.NewWriter(os.Stdout)
header:= my_struct.GetHeaders()
values:= my_struct.ToSlice()
if err:= w.Write(headers); err!= nil {
//写入失败做某事
}
if err:= w.Write(values); err!= nil {
//写入失败做某事
}

如果这是没有道理的让我知道,我可以跟进代码样本的两种方法之一。


What is an idiomatic golang way to dump the struct into a csv file provided? I am inside a func where my struct is passed as interface{}:

func decode_and_csv(my_response *http.Response, my_struct interface{})

Why interface{}? - reading data from JSON and there could be a few different structs returned, so trying to write a generic enough function.

an example of my types:

type Location []struct {
    Name                   string `json: "Name"`
    Region                 string `json: "Region"`
    Type                   string `json: "Type"`
}

解决方案

It would be a lot easier if you used a concrete type. You'll probably want to use the encoding/csv package, here is a relevant example; https://golang.org/pkg/encoding/csv/#example_Writer

As you can see, the Write method is expecting a []string so in order to generate this, you'll have to either 1) provide a helper method or 2) reflect my_struct. Personally, I prefer the first method but it depends on your needs. If you want to go route two you can get all the fields on the struct an use them as the column headers, then iterate the fields getting the value for each, use append in that loop to add them to a []string and then pass it to Write out side of the loop.

For the first option, I would define a ToSlice or something on each type and then I would make an interface call it CsvAble that requires the ToSlice method. Change the type in your method my_struct CsvAble instead of using the empty interface and then you can just call ToSlice on my_struct and pass the return value into Write. You could have that return the column headers as well (meaning you would get back a [][]string and need to iterate the outer dimension passing each []string into Write) or you could require another method to satisfy the interface like GetHeaders that returns a []string which is the column headers. If that were the case your code would look something like;

w := csv.NewWriter(os.Stdout)
headers := my_struct.GetHeaders()
values := my_struct.ToSlice()
if err := w.Write(headers); err != nil {
    //write failed do something
}
if err := w.Write(values); err != nil {
    //write failed do something
}

If that doesn't make sense let me know and I can follow up with a code sample for either of the two approaches.

这篇关于将结构写入csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆