我有一小段代码让我整个周末都很忙。package mainimport ( "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 中看到的,但我得到了这个:Unquoted string,"Cr@zy text with , and \ and "" etc"Quoted string,"""Cr@zy text with , and \\ and \"" etc"""这些额外的引号来自哪里,我该如何避免它们?我尝试了很多方法,包括使用 strings.Quote 和一些类似的方法,但我似乎找不到完美的解决方案。请帮助?
3 回答
一只甜甜圈
TA贡献1836条经验 获得超5个赞
我有带有双引号字符串的 csv 文件,例如:
text;///*[@class="price"]/span;text
并且 csv Reader 生成错误以读取 csv 文件。有帮助的是:
reader := csv.NewReader(file)
reader.LazyQuotes = true
- 3 回答
- 0 关注
- 275 浏览
添加回答
举报
0/150
提交
取消