在golang中编写csv时如何删除 BOM删除

问题描述 投票:0回答:1

我正在用一个csv文件编写一个统计信息,以了解我的golang服务器中的传入直径流量,但该文件在rach行的开头包含一个“”字符。

01.; 34642222231118599998; 21; 6588283272 | 6588283272 | 300 | 0 | 46692 | 1582611861 |,|| 2001 | 01.; 34642222231118599998; 21; 6588283272 | gytwocsdr.circles.asia | circles.asia | 0 | 1 | 1582611861**** 01.; 34642222231118599998; 22; 6588080153 | 6588080153 | 300 | 0 | 46692 | 1582611861 |,|| 2001 | 01.; 34642222231118599998; 22; 6588080153 | gytwocsdr.circles.asia | circles.asia | 0 | 1 | 1582611861**** 01。;; 34642222231118599998; 23; 6587508893 | 6587508893 | 300 | 0 | 46692 | 1582611861 |,|| 2001 | 01.; 34642222231118599998; 23; 6587508893 | gytwocsdr.circles.asia | circles.asia | 0 | 1 | 1582611861

请指导我如何解决此问题。

stats.go >>

     package main

     import (
       "encoding/csv"
       "os"
     )

   var (
        filename = "stats.csv"
     )

    // write the request and response to csv file
     func updateCSVFile(req string, resp string) error {
      file, err := os.OpenFile("/home/gyuser/charging-control-engine/stats/stats.csv",    os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
    if err != nil {
            return err
      }
    defer file.Close()

    // solve the encoding problem
    file.WriteString("\xEF\xBB\xBF")
    writer := csv.NewWriter(file)
    defer writer.Flush()
    return writer.Write([]string{req, resp})
     }

ccr.go >>>

       package main

    import (
      "log"
      "time"
      "github.com/fiorix/go-diameter/v4/diam/sm"
      "github.com/fiorix/go-diameter/v4/diam"
      "github.com/fiorix/go-diameter/v4/diam/avp"
      )

   const (
    // CCRInitial - ccr request type
    CCRInitial = 1
    // CCRUpdate - ccr request type
    CCRUpdate = 2
    // CCRTermination - ccr request type
    CCRTermination = 3
    // VM - flags VM-
    VM = avp.Mbit | avp.Vbit
    // M - flags M-
    M = avp.Mbit
    // TGPP - vendor id for TGPP
    TGPP = 10415
   )



  // Decoder for command requests
   type Decoder interface {
    Decode() (*diam.Message, error)
    WriteCSV() error
   }


  // CCRGxDecoder try to parse the gx requests with ccr
   type CCRGxDecoder struct {
    msg   *diam.Message
    gx    *GxStruct
    r     *CCRGxRequest
    q     *CCRGxResponse
    auth  bool
    start time.Time
    }


   // handle the Credit Control Request(CCR)
    func handleCCR(s *sm.Settings, conf *Config) diam.HandlerFunc {
    return func(c diam.Conn, m *diam.Message) {
            var decoder Decoder
            // application id for Gx and Gy
            //if m.Header.ApplicationID == conf.Gx.ID {
            //      decoder = NewCCRGxDecoder(m, &conf.Gx, conf.Server.Auth)
            //} else 
            if m.Header.ApplicationID == conf.Gy.ID {
                    decoder = NewCCRGyDecoder(m, &conf.Gy, conf.Server.Auth)
            } else {
                    log.Printf("invalid application id: %v\n", m.Header.ApplicationID)
                    return
            }

            // decode the requests and make the answer message
            nm, err := decoder.Decode()
            if err != nil {
                    log.Printf("decode failed: %v, %v\n", err, m)
                    return
            }

            // write the message back to the peer
            if _, err := nm.WriteTo(c); err != nil {
                    log.Printf("failed to write message: %v\n", err)
                    return
            }

            // update the request and response to csv file
            if err := decoder.WriteCSV(); err != nil {
                    log.Printf("write csv: %v\n", err)
            }


        }
     }
csv go byte-order-mark
1个回答
0
投票
file.WriteString("\xEF\xBB\xBF")

只需从代码中删除此行。这是UTF-8编码的BOM,与您看到的<feff>完全相同。

© www.soinside.com 2019 - 2024. All rights reserved.