drjyvoi734793
2017-04-16 10:16
浏览 136
已采纳

如何在golang中将GUID原始16个字节写入CSV?

I have following code, which try save UUID raw 16 bytes (with 0x0A inside) to CSV format

package main

import (
    "encoding/csv"
    "github.com/satori/go.uuid"
    "log"
    "os"
)

func main() {
    u, err := uuid.FromString("e1393c62-877a-4adc-8ffb-f1bf0a337c5f")
    if err != nil {
        log.Fatal(err)
    }
    csv_file, err := os.OpenFile("csv_wtf.csv", os.O_WRONLY|os.O_CREATE, 0644)
    if err != nil {
        log.Fatal(err)
    }
    s := string(u.Bytes())
    log.Printf("len(s)=%d",len(s))
    csv_writer := csv.NewWriter(csv_file)
    csv_writer.UseCRLF = false
    csv_writer.Write([]string{s})
    csv_writer.Flush()
    finfo, err := csv_file.Stat()
    if err != nil {
        log.Fatal(err)
    }
    log.Printf("size csv_wtf.csv = %d", finfo.Size())
    csv_file.Close()
}

this code output data to csv with add extra bytes

2017/04/16 12:37:14 len(s)=16
2017/04/16 12:37:14 size csv_wtf.csv = 29

why encoding/csv add extra bytes when follow my string over range (see https://golang.org/src/encoding/csv/writer.go#L38, https://golang.org/src/encoding/csv/writer.go#L50 and https://golang.org/src/encoding/csv/writer.go#L76)?

could somebody help me find CSV package who don't do it strange conversion ??

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

我有以下代码,该代码尝试将UUID原始16个字节(内部为0x0A)保存为CSV格式</ p>

 包main 
 
import(
“ encoding / csv” 
“ github.com/satori/go.uuid” 
“ log” 
“ os” 
)  
 
func main(){
u,err:= uuid.FromString(“ e1393c62-877a-4adc-8ffb-f1bf0a337c5f”)
 if err!= nil {
 log.Fatal(err)
} \  n csv_file,err:= os.OpenFile(“ csv_wtf.csv”,os.O_WRONLY | os.O_CREATE,0644)
如果err!= nil {
 log.Fatal(err)
} 
s:=字符串 (u.Bytes())
 log.Printf(“ len(s)=%d”,len(s))
 csv_writer:= csv.NewWriter(csv_file)
 csv_writer.UseCRLF = false 
 csv_writer。  Write([] string {s})
 csv_writer.Flush()
 finfo,err:= csv_file.Stat()
如果err!= nil {
 log.Fatal(err)
} 
 log  .Printf(“ size csv_wtf.csv =%d”,finfo.Size())
 csv_file.Close()
} 
 </ code> </ pre> 
 
 

此代码输出数据 到csv中并添加额外的字节</ p>

  2017/04/16 12:3  7:14 len = 16 
2017 / 04/16 12:37:14 size csv_wtf.csv = 29 
 </ code> </ pre> 
 
 

为什么编码/ csv添加额外的字节 当我的字符串超出范围时(请参见 https://golang.org/src/ encoding / csv / writer.go#L38 https:// golang.org/src/encoding/csv/writer.go#L50 https://golang.org/src/encoding/csv/writer.go#L76 )?</ p>

有人可以帮助我找到不这样做的CSV软件包吗? 转换很奇怪?? </ p> </ div>

1条回答 默认 最新

相关推荐 更多相似问题