draxu26480
2018-08-16 12:07
浏览 42
已采纳

总结csv的内容

Context I'm working on creating a little program that can summarize the contents of an absolute mess of a bill, which is in csv form.

The bill has three columns I'm interested in:

  1. Event type. Here, I'm only interested in the rows where this column reads CHARGE
  2. The cost. Self explanatory.
  3. Resource name, containing Server and cluster names. The format is servername.clustername.

The idea is to select the rows that are labeled as charge, split them up first by cluster and then by server name, and sum up the total costs for each.

I can't help but feel like this should be easy, but I've been scratching my head on this for a while now, and just can't seem to figure it out. At this point I ought to state that I am fairly new to programming and entirely new to GO.

Here's what I have so far:

package main

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



func main() {
    rows := readBill("bill-2018-April.csv")
    rows = calculateSummary(rows)
    writeSummary("bill-2018-April-output", rows)

}

func readBill(name string) [][]string {

    f, err := os.Open(name)

    if err != nil {
        log.Fatalf("Cannot open '%s': %s
", name, err.Error())
    }

    defer f.Close()

    r := csv.NewReader(f)

    rows, err := r.ReadAll()

    if err != nil {
        log.Fatalln("Cannot read CSV data:", err.Error())
    }

    return rows
}

type charges struct {
    impactType string
    cost       float64
    resName    string
}
func createCharges(rows [][]string){
    charges:= []charges{}
    for i,r:=range rows {
        var c charges
        c.impactType :=r [i][10]
        c.cost := r [i][15]
        c.resName := r [i][20]
        charges = append()
    }
    return charges
} 

So, as far as I can tell, I should now have isolated the columns I am interested in (i.e. columns 10, 15 and 20). Is what I have so far even correct?

How would I go about singling out the rows reading "CHARGE" and slicing everything up by cluster and server?

Summing things up shouldn't be too tricky, but for whatever reason, this is really stumping me.

图片转代码服务由CSDN问答提供 功能建议

上下文 我正在创建一个小程序,可以总结以下内容

该票据有我感兴趣的三列:

  1. 事件类型。 在这里,我只对本列读取CHARGE
  2. 费用的行感兴趣。 不言自明。
  3. 资源名称,包含服务器和群集名称。 格式为servername.clustername。

    该想法是选择标记为charge的行,先按群集再按服务器名称将其拆分,然后 总结每个项目的总成本。

    我忍不住觉得这应该很容易,但是我已经花了很长时间了,而且可以 似乎没有弄清楚。 在这一点上,我应该声明自己对编程来说是一个新手,而对GO则是一个全新的人。

    这是我到目前为止所拥有的:

     < 代码>包main 
     
    import(
    “ encoding / csv” 
    “ log” 
    “ os” 
    “ sort” 
    “字符串” 
    )
     
     
     
    func main()  {
    行:= readBill(“ bill-2018-April.csv”)
    行= calculateSummary(行)
     writeSummary(“ bill-2018-April-output”,行)
     
    } 
     \  nfunc readBill(名称字符串)[] []字符串{
     
    f,err:= os.Open(name)
     
    如果err!= nil {
     log.Fatalf(“无法打开'%s':  %s 
    “,名称,错误err.Error())
    } 
     
    推迟f.Close()
     
    r:= csv.NewReader(f)
     
    行,错误err:= r。  ReadAll()
     
    ,如果错误!= nil {
     log.Fatalln(“无法读取CSV数据:”,err.Error())
    } 
     
    返回行
    } 
     
    键入费用 struct {
     impactType string 
     cost float64 
     resName string 
    } 
    func createCharges(rows [] [] string){
     charge:= [] charges {} 
     for i,r:= range rows {  
     var c收费
     c.impactType:= r  [i] [10] 
     c.cost:= r [i] [15] 
     c.resName:= r [i] [20] 
    费用= append()
    } 
    返回费用
      } 
       
     
     

    据我所知,我现在应该隔离出我感兴趣的列(即 第10、15和20列)。

    我到目前为止该怎么办? 我该如何选择读出“ CHARGE”的行并按集群和服务器划分所有内容?

    总结一下事情应该不会太棘手,但是无论出于什么原因,这真的让我很沮丧。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dpkt31779 2018-08-16 12:42
    已采纳

    Just use two maps to store the sums per server and per cluster. And since you're not interested in the whole CSV but only some rows, reading everything is kind of wasteful. Just skip the rows you don't care about:

    package main
    
    import (
        "encoding/csv"
        "fmt"
        "io"
        "log"
        "strconv"
        "strings"
    )
    
    func main() {
        b := `
    ,,,,,,,,,,CHARGE,,,,,100.00,,,,,s1.c1
    ,,,,,,,,,,IGNORE,,,,,,,,,,
    ,,,,,,,,,,CHARGE,,,,,200.00,,,,,s2.c1
    ,,,,,,,,,,CHARGE,,,,,300.00,,,,,s3.c2
    `
    
        r := csv.NewReader(strings.NewReader(b))
    
        byServer := make(map[string]float64)
        byCluster := make(map[string]float64)
    
        for i := 0; ; i++ {
            row, err := r.Read()
            if err == io.EOF {
                break
            }
            if err != nil {
                log.Fatal(err)
            }
    
            if row[10] != "CHARGE" {
                continue
            }
    
            cost, err := strconv.ParseFloat(row[15], 64)
            if err != nil {
                log.Fatalf("row %d: malformed cost: %v", i, err)
            }
    
            xs := strings.SplitN(row[20], ".", 2)
            if len(xs) != 2 {
                log.Fatalf("row %d: malformed resource name", i)
            }
    
            server, cluster := xs[0], xs[1]
    
            byServer[server] += cost
            byCluster[cluster] += cost
        }
    
        fmt.Printf("byServer: %+v
    ", byServer)
        fmt.Printf("byCluster: %+v
    ", byCluster)
    }
    
    // Output:
    // byServer: map[s2:200 s3:300 s1:100]
    // byCluster: map[c1:300 c2:300]
    

    Try it on the playground: https://play.golang.org/p/1e9mJf4LyYE

    点赞 打赏 评论

相关推荐 更多相似问题