登录
首页 >  Golang >  Go问答

去除 golang 中写入的 csv 文件中的 BOM

来源:stackoverflow

时间:2024-03-15 08:54:26 149浏览 收藏

本篇文章给大家分享《去除 golang 中写入的 csv 文件中的 BOM》,覆盖了Golang的常见基础知识,其实一个语言的全部知识点一篇文章是不可能说完的,但希望通过这些问题,让读者对自己的掌握程度有一定的认识(B 数),从而弥补自己的不足,更好的掌握它。

问题内容

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

01.;3464222231118599998;21;6588283272|6588283272|300|0|46692|1582611861|,|2001|01.;34642222231118599998;21;6588283272|gytwo csdr.circles.asia|circles.asia|0|1|1582611861 ****01.;3464222231118599998;22;6588080153|6588080153|300|0|46692|1582611861|,|2001|01.;34642222231118599998;22;6588080153|gytwoc sdr.circles.asia|circles.asia|0|1| 1582611861 ****01.;3464222231118599998;23;6587508893|6587508893|300|0|46692|1582611861|,|2001|01.;34642222231118599998;23;6587508893|gytwoc sdr.circles.asia|circles.asia|0|1| 1582611861

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

统计.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)
            }


        }
     }

解决方案


file.WriteString("\xEF\xBB\xBF")

只需从代码中删除此行即可。这是utf-8编码下的bom,和你看到的一模一样。

以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>