file print extra spaces

Alex Willmer alex at moreati.org.uk
Tue Mar 22 21:50:47 EDT 2011


On Mar 23, 1:33 am, monkeys paw <mon... at joemoney.net> wrote:
> When i open a file in python, and then print the
> contents line by line, the printout has an extra blank
> line between each printed line (shown below):
>
>  >>> f=open('authors.py')
>  >>> i=0
>  >>> for line in f:
>         print(line)
>         i=i+1
>         if i > 14:
>                 break
>
> author_list = {
>
>                    '829337' : {
>
>                                  'author_name' : 'Carter, John',
>
>                                  'count' : 49,
>
>                                  'c2' : '0.102040816326531',
>
> How can i print the file out without having an extra newline between
> printed lines?
>
> Thanks for the help all.

What you are seeing is 1 newline from the original file, and a second
newline from the print() function. To resolve this use either
print(line, end='') or sys.stdout.write(line).

Regards, Alex



More information about the Python-list mailing list