简体   繁体   中英

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

2: 7840231,70,60,85.4

3: 3845913,55.5,60.5,80.0

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.

Use print(argument, end='') .

If you want to streamline your code, you should look into the enumerate built in function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM