print(f) for files .. and is print % going away?

E

Esmail

Hello all,

I use the print method with % for formatting my output to
the console since I am quite familiar with printf from my
C days, and I like it quite well.

I am wondering if there is a way to use print to write
formatted output to files?

Also, it seems like I read that formatting with print is
going away eventually and we should start using something
else? Is that true and if so, what?

Thanks,
Esmail
 
E

Esmail

Hi Duncan,

Thanks for the information, I'll dig deeper :)

(for some reason I can't get the from __future__ import
to work,
File "<stdin>", line 1

SyntaxError: future feature print_function is not defined

but I am probably making some silly mistake, plus
I have been meaning to find out more about this too, so
this is a good chance to learn something new).

Thanks again!

Esmail
 
E

Esmail

You can only use the print function on 2.6 and later. If you have an older
version of Python then you'll get that error.

Ooops, yes, you wrote that and I tried with 2.6 under Windows
(my Ubuntu has 2.5) .. I must have typed something wrong when I
tried this with 2.6 because it's working fine now :)

Thanks again Duncan,

Esmail
 
L

Lie Ryan

Esmail said:
Hello all,

I use the print method with % for formatting my output to
the console since I am quite familiar with printf from my
C days, and I like it quite well.

There has never been print-with-formatting in python, what we have is
the % string substitution operator, which is a string operation instead
of print operation.
I am wondering if there is a way to use print to write
formatted output to files?

Of course:

f = open(...)
open.write('%s' % foo)

or

f = open(...)
print > f, '%s' % foo

or in python 3:
f = open(...)
print('%s' % foo, file=f)

or using the new superpowerful str.format()'foo: 123 bar: 123.5 4.567890e+02'
 
E

Esmail

hello,

Lie said:
There has never been print-with-formatting in python, what we have is
the % string substitution operator, which is a string operation instead
of print operation.

Yes, I see that now, thanks for clarifying it. I guess I thought
so because I've always associated the % formatting with printing.
Of course:

f = open(...)
open.write('%s' % foo)

very nice ..
or using the new superpowerful str.format()
'foo: 123 bar: 123.5 4.567890e+02'

I'll have to check it out - thanks again,

Esmail
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top