gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 22:32:50 28 4
gpt4 key购买 nike

我在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)
}


}
}

最佳答案

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

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

关于csv - 如何在golang中编写csv时删除<feff> BOM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60126872/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com