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

huangapple go评论89阅读模式
英文:

Why does "fmt.Sprintf("%v,", lines[i])" put the comma on new line?

问题

这是完整的代码:

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

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

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

我猜测问题出在lines[i]必须以换行字节结尾的字符串上。我尝试过将其移除,但失败了。

我加载的文件只是 JSON 文件,例如:

第一行:{ "foo":"bar","baz":null }

第二行:{ "foo":"bar","baz":"quz" }

我想在所有行的末尾添加逗号。任何帮助将不胜感激。

只是为了让自己更清楚一些,我现在得到的结果是:

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

而我想要得到的结果是:

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

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), "\n")

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

	output := strings.Join(lines, "\n")
	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" },

答案1

得分: 3

你的 JSON 数据是否来自 Windows 平台,并且实际上包含了 /r/n 而不仅仅是 /n?你可以在这个 playground 示例 中看到这种行为:

package main

import (
	"fmt"
	"strings"
)

func main() {
	a := "test\r\nnewtest\r\ntest2"
	b := strings.Split(a, "\n")
	c := strings.Join(b, ",\n")
	fmt.Printf("%v", c)
}
英文:

Is it possible that your JSON data is coming from Windows and actually contains /r/n rather than just /n?

You can see this behaviour using /r/n in this playground example:

package main

import (
	"fmt"
	"strings"
)

func main() {
	a := "test\r\nnewtest\r\ntest2"
	b := strings.Split(a, "\n")
	c := strings.Join(b, ",\n")
	fmt.Printf("%v", c)
	
}

答案2

得分: 3

尝试修剪该行,以清除其末尾的Unicode代码点:

import "strings"

// ...

for _, line := range lines {
    line = fmt.Sprintf("%v,", strings.Trim(line, " \r\n"))
}
英文:

Try trimming the line to clean up whatever trailing unicode code points it has:

import "strings"

// ...

for _, line := range lines {
        line = fmt.Sprintf("%v,", strings.Trim(line, " \r\n"))
}

huangapple
  • 本文由 发表于 2017年3月5日 22:10:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/42609375.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定