转到:为什么“ fmt.Sprintf(”%v,”,lines [i])”将逗号放在新行上?

This is the full code:

files, _ := ioutil.ReadDir("files")
for _, f := range files {
    input, err := ioutil.ReadFile("files/" + f.Name())
    lines := strings.Split(string(input), "
")

    for i, _ := range lines {
        lines[i] = fmt.Sprintf("%v,", lines[i])
    }

    output := strings.Join(lines, "
")
    err = ioutil.WriteFile("files/"+f.Name()+"fix", []byte(output), 0644)
    if err != nil {
        log.Fatalln(err)
    }
}

I assume it is because lines[i] must contain a newline byte at the end of the string.. I have tried to remove it but failed..

The files I load are just json files e.g.

line 1: { "foo":"bar","baz":null }

line 2: { "foo":"bar","baz":"quz" }

Where I am trying to add a comma to the end of all lines.. any help would be much appreciated

Just to make myself a little more clear, what I get now is:

{ "foo":"bar","baz":null }
,
{ "foo":"bar","baz":"quz" }
,

whereas what I want to get is:

{ "foo":"bar","baz":null },
{ "foo":"bar","baz":"quz" },
查看全部
doujia1904
doujia1904
2017/03/05 14:10
  • it技术
  • 互联网问答
  • IT行业问题
  • 编程语言问答
  • 计算机技术
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

2个回复