为什么使用end =“”时输出仍包含新行

时间:2018-08-13 13:19:05

标签: python file while-loop

尽管使用end="",为什么输出中仍包含换行符

with open("mydata.txt", mode = "w", encoding = "utf-8") as myfile:
    myfile.write("myname is \nTimduudy\nIm from eggyork\nrefff")


with open("mydata.txt", encoding = "utf-8") as myfile:
    line_num = 1
    while True:
        line = myfile.readline()
        if not line:
            break
        print("Line", line_num, ":", line, end="")
        line_num += 1

输出:

Line 1 : myname is 
Line 2 : Timduudy
Line 3 : Im from eggyork
Line 4 : refff

2 个答案:

答案 0 :(得分:3)

您的问题是由于以下事实造成的:table1 = 'ID2'方法将换行符'\ n'保留在每一行的末尾。要剥离它,可以执行以下操作:

table2

您也可以不带任何参数使用_ctx.ChangeLogs.Where(changeLog => changeLog.LogDetails .Where(detail => detail.Priority > 0) .GroupBy(x => x.Priority) .All(grp => grp.Count() == 1) ); ,但也会删除结尾的空格。

答案 1 :(得分:0)

请尝试line = myfile.readline().strip(),这将从每行中删除换行符。