File Iterator - Print - Double New Lines

Mike Brenner mikeb at mitre.org
Sat Jun 1 20:35:01 EDT 2002


Hi Peter,

That is not what happens, though. The first three outputs (from the first call to reader) do not get newlines. The second three outputs (from the second call to reader) get double newlines. The explanation that "the print statement adds newlines after every lines it prints" would explain the double newlines the second time, but it does not explain the lack of newlines the first time. Did you run it on a windows machine?

Mike


Peter wrote:
Mike, the print statement adds newlines after every line it prints.
If you want to see the real contents of the file, use sys.stdout.write().
Alternatively, you might want to strip the newlines from each line
as you read them in.  Depends what you are trying to do.



MIKE's OUTPUT (CONTAINING HIS QUESTION):

QUESTION: How to get SINGLE SPACING?
Using Python 2.2.1, it does the same under win32all build 142 and
      running it from a DOS box without the windows extensions.
trying it without the \n
abc
trying it with the \n
a

b

c


MIKE's PROGRAM:

print "QUESTION: Why did it jump from NO LINE SPACING to DOUBLE SPACING?"
print "QUESTION: How to get SINGLE SPACING?"
print
print "Using Python 2.2.1, it does the same under win32all build 142 and
"
print "      running it from a DOS box without the windows extensions."

def writer(list):
    f=open("tmp.tmp","w")
    for item in list:
        f.write(item)
    f.close()

def reader():
    f=open("tmp.tmp","r")
    for line in f:
        print line
    f.close()

print "trying it without the \\n"
writer(["a","b","c"])
reader()

print "trying it with the \\n"
writer(["a\n","b\n","c\n"])
reader()abc
trying it with the \n
a

b

c






More information about the Python-list mailing list