PDA

View Full Version : Python Single Spaced Output?




CaptainZap
Feb 14, 2008, 06:53 PM
How come it seems when Python outputs things to Terminal, everything is double spaced? Like this is the output from Python
http://img523.imageshack.us/img523/5922/picture4ta5.png

And this is from the cat command
http://img175.imageshack.us/img175/634/picture5ik1.png

And yes, it is an ice cream cone. Anyways, is there a way to change this?



kainjow
Feb 14, 2008, 07:48 PM
Post some code?

CaptainZap
Feb 14, 2008, 07:54 PM
If you had this in a text file
.-"`'"-.
/ \
| |
/'---'--`\
| |
\.--.---.-./
(_.--._.-._)
\=-=-=-/
\=-=-/
\=-/
\/

And then used this Python code
for line in open('textfile.txt','r'):
print line


It looks like the first picture, and then this looks like the second picture

cat textfile.txt

kainjow
Feb 14, 2008, 07:58 PM
From the Python Reference Manual (http://docs.python.org/ref/print.html):

A "\n" character is written at the end, unless the print statement ends with a comma.

So change your code to:

for line in open('textfile.txt','r'):
print line,

CaptainZap
Feb 14, 2008, 08:02 PM
Well played sir, thank you. I had read that too, I just thought it allowed you to write to the same line :P

kainjow
Feb 14, 2008, 08:07 PM
I had read that too, I just thought it allowed you to write to the same line :P

It does if you add the comma. But "line" already contains a \n from the text file, so that is creating the new line.