Go编码/csv中带引号的字符串的奇怪CSV结果 [英] Strange CSV result for quoted strings in go encoding/csv

查看:94
本文介绍了Go编码/csv中带引号的字符串的奇怪CSV结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有那么一点代码,使我整个周末都很忙.

I have this little bit of code that kept me busy the whole weekend.

package main

import (
    "encoding/csv"
    "fmt"
    "log"
    "os"
)

func main() {
    f, err := os.Create("./test.csv")
    if err != nil {
        log.Fatal("Error: %s", err)
    }
    defer f.Close()

    w := csv.NewWriter(f)
    var record []string
    record = append(record, "Unquoted string")
    s := "Cr@zy text with , and \\ and \" etc"
    record = append(record, s)
    fmt.Println(record)
    w.Write(record)

    record = make([]string, 0)
    record = append(record, "Quoted string")
    s = fmt.Sprintf("%q", s)
    record = append(record, s)
    fmt.Println(record)
    w.Write(record)

    w.Flush()
}

运行时它会打印出来:

[Unquoted string Cr@zy text with , and \ and " etc]
[Quoted string "Cr@zy text with , and \\ and \" etc"]

带引号的第二个文本恰好是我希望在CSV中看到的文本,但是我得到了:

The second, quoted text is exactly what I would wish to see in the CSV, but instead I get this:

Unquoted string,"Cr@zy text with , and \ and "" etc"
Quoted string,"""Cr@zy text with , and \\ and \"" etc"""

那些多余的报价从哪里来?我如何避免使用它们?我已经尝试了很多方法,包括使用strings.Quote之类的方法,但是我似乎找不到完美的解决方案.请帮忙吗?

Where do those extra quotes come from and how do I avoid them? I have tried a number of things, including using strings.Quote and some such but I can't seem to find a perfect solution. Help, please?

推荐答案

这是将数据存储为CSV的标准的一部分.出于分析原因,必须转义双引号字符.

It's part of the standard for storing data as CSV. Double quote characters need to be escaped for parsing reasons.

字段中的(双)引号字符必须由两个(双)引号字符表示.
A (double) quote character in a field must be represented by two (double) quote characters.

来自: http://en.wikipedia.org/wiki/Comma-separated_values

您不必担心,因为CSV阅读器会取消转义双引号.

You don't really have to worry because the CSV reader un-escapes the double quote.

package main

import (
    "encoding/csv"
    "fmt"
    "os"
)
func checkError(e error){
    if e != nil {
        panic(e)
    }
}
func writeCSV(){
    fmt.Println("Writing csv")
    f, err := os.Create("./test.csv")
    checkError(err)
    defer f.Close()

    w := csv.NewWriter(f)
    s := "Cr@zy text with , and \\ and \" etc"
    record := []string{ 
      "Unquoted string",
      s,
    }
    fmt.Println(record)
    w.Write(record)

    record = []string{ 
      "Quoted string",
      fmt.Sprintf("%q",s),
    }
    fmt.Println(record)
    w.Write(record)
    w.Flush()
}
func readCSV(){
    fmt.Println("Reading csv")
    file, err := os.Open("./test.csv")
    defer file.Close();
    cr := csv.NewReader(file)
    records, err := cr.ReadAll()
    checkError(err)
    for _, record := range records {
        fmt.Println(record)
    }
}
func main() {
   writeCSV()
   readCSV()
}

输出

Writing csv
[Unquoted string Cr@zy text with , and \ and " etc]
[Quoted string "Cr@zy text with , and \\ and \" etc"]
Reading csv
[Unquoted string Cr@zy text with , and \ and " etc]
[Quoted string "Cr@zy text with , and \\ and \" etc"]

这是写功能的代码. func(w * Writer)Write(record []字符串)(错误错误)

这篇关于Go编码/csv中带引号的字符串的奇怪CSV结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆