[英]Printing each line of a file out
file = open(filename, 'r')
a = 1
for i in file:
print(str(a) + ': ' + str(i))
a += 1
I am trying to print each line of the file out. 我正在尝试打印文件的每一行。 yet as I print it the output is:
在我打印时,输出为:
1: 9874234,12.5,23.0,50.0 1:9874234,12.5,23.0,50.0
2: 7840231,70,60,85.4 2:7840231,70,60,85.4
3: 3845913,55.5,60.5,80.0 3:3845913,55.5,60.5,80.0
4: 3849511,20,60,50 4:3849511,20,60,50
This is the correct output but I want to remove the extra lines between each output so there is no space between each output 这是正确的输出,但是我想删除每个输出之间的多余行,因此每个输出之间没有空格
Your lines already end with a linebreak. 您的行已经以换行符结束。
print
is adding another one. print
正在添加另一个。
Use print(argument, end='')
. 使用
print(argument, end='')
。
If you want to streamline your code, you should look into the enumerate
built in function. 如果要简化代码,则应查看内置的
enumerate
函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.