如何从行中删除\ n [英] How remove \n from lines

查看:82
本文介绍了如何从行中删除\ n的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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...
        }

在每行末尾添加\ n,结束程序正常工作,仅在最后一行工作...

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.

推荐答案

您可以分割最后一个字符:

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")

这篇关于如何从行中删除\ n的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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