如何从文本行中删除换行符(\n)?

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

How remove \n from lines

问题

以下是您提供的代码的翻译:

file, _ := os.Open("x.txt")
f := bufio.NewReader(file)

for {
    read_line, _ := ReadString('\n')
    fmt.Print(read_line)
    
    // 其他处理解析行的代码...
}

end it add \n on every line , end program to work , only work with last line...

Please put example, i try anything end any solution what i find here not work for me.

请注意,我只翻译了您提供的代码部分,其他内容我将忽略。

英文:
file, _ := os.Open("x.txt")
	f := bufio.NewReader(file)
	
	for {
		read_line, _ := ReadString('\n')
        fmt.Print(read_line)
        
        
        // other code what work with parsed line...
        }

end it add \n on every line , end program to work , only work with last line...

Please put example, i try anything end any solution what i find here not work for me.

答案1

得分: 80

你可以截取最后一个字符:

read_line = read_line[:len(read_line)-1]

也许更好的方法是使用strings库:

read_line = strings.TrimSuffix(read_line, "\n")
英文:

You can slice off the last character:

read_line = read_line[:len(read_line)-1]

Perhaps a better approach is to use the strings library:

read_line = strings.TrimSuffix(read_line, "\n")

答案2

得分: 25

如果你想逐行读取文件,使用bufio.Scanner会更容易。Scanner不会将endline\n\r\n)包含在行中。

file, err := os.Open("yourfile.txt")
if err != nil {
    //处理错误
    return
}
defer file.Close()

s := bufio.NewScanner(file)
for s.Scan() {
    read_line := s.Text()

    //处理解析后的行的其他代码...
}

如果你想去掉endline,我建议你使用TrimRightFunc,例如:

read_line = strings.TrimRightFunc(read_line, func(c rune) bool {
    //在Windows中,换行符是\r\n
    return c == '\r' || c == '\n'
})

更新:
正如@GwynethLlewelyn指出的那样,使用TrimRight会更简单(更清晰),例如:

read_line = strings.TrimRight(read_line, "\r\n")

在内部,TrimRight函数调用TrimRightFunc,如果匹配TrimRight的第二个参数中的任何字符,将删除该字符。

英文:

If you want to read a file line-by-line, using bufio.Scanner will be easier. Scanner won't includes endline (\n or \r\n) into the line.

file, err := os.Open("yourfile.txt")
if err != nil {
	//handle error
	return
}
defer file.Close()

s := bufio.NewScanner(file)
for s.Scan() {
	read_line := s.Text()

	// other code what work with parsed line...
}

If you want to remove endline, I suggest you to use TrimRightFunc, i.e.

read_line = strings.TrimRightFunc(read_line, func(c rune) bool {
    //In windows newline is \r\n
    return c == '\r' || c == '\n'
})

Update:
As pointed by @GwynethLlewelyn, using TrimRight will be simpler (cleaner), i.e.

 read_line = strings.TrimRight(read_line, "\r\n")

Internally, TrimRight function call TrimRightFunc, and will remove the character if it match any character given as the second argument of TrimRight.

答案3

得分: 13

清晰简洁的方法

你也可以使用 strings.TrimSpace() 函数,它会移除任何额外的尾部或首部字符,比如正则表达式中的 \n\r,这种方法更加清晰简洁。

read_line = strings.TrimSpace(read_line)

Go文档:https://pkg.go.dev/strings#TrimSpace

英文:

Clean and Simple Approach

You can also use the strings.TrimSpace() function which will also remove any extra trailing or leading characters like the ones of regex \n, \r and this approach is much more cleaner.

read_line = strings.TrimSpace(read_line)

Go doc: https://pkg.go.dev/strings#TrimSpace

huangapple
  • 本文由 发表于 2017年6月9日 10:34:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/44448384.html
匿名

发表评论

匿名网友

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

确定