How to avoid new line in readline() function in python 3x?

This question already has an answer here:

  • How to print without newline or space? 26 answers

  • 由于你的内容已经包含你想要的换行符,所以通过使用可选的end参数告诉print()函数不要添加任何内容:

    def print_one_line(line_number,f):
        print(line_number,f.readline(), end='')
    

    而不是跳过输出的换行符,您可以从输入中去除它:

    print(line_number, f.readline().rstrip('n'))
    

    Beside the other ways, you could also use:

    import sys
    sys.stdout.write(f.readline())
    

    Works with every Python version to date.

    链接地址: http://www.djcxy.com/p/77218.html

    上一篇: 如何水平打印字符?

    下一篇: 如何避免python 3x中readline()函数中的新行?