且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用print而不是file.write(str)

更新时间:2023-12-04 12:18:04

AMaécrit:


我发现打印方法更加灵活。我可以使用print而不是file.write方法吗?
Hi,
I found print much more flexible that write method. Can I use print instead
of file.write method?




f = open(&/; / path / to / file")

print>> f,这是我的%s消息 %" first"

f.close()


要打印到stderr:


import sys

打印>> sys.stderr,oops


FWIW,你在任何地方使用字符串格式化,不仅仅是在打印语句中:


s ="一些%s和%格式化 %(不错,酷)

打印s


您还可以使用dict formating:


names = {" other":" AM"," me" :" bruno"}

s =" hello%(other)s,my name is%(me)s" %name



f = open("/path/to/file")
print >> f, "this is my %s message" % "first"
f.close()

To print to stderr:

import sys
print >> sys.stderr, "oops"

FWIW, you and use string formating anywhere, not only in print statements:

s = "some %s and % formating" % ("nice", "cool")
print s

You can also use "dict formating":

names = {"other": "A.M.", "me" : "bruno"}
s = "hello %(other)s, my name is %(me)s" % names


不知道>>语法:很可爱了解它Bruno - 谢谢

你。


到OP - 我发现print语句对于类似的东西很有用:

print''this'',''是'','a'',''test''
Didn''t know of the >> syntax: lovely to know about it Bruno - thank
you.

To the OP - I find the print statement useful for something like:
print ''this'',''is'',''a'',''test''
''这是一个测试''
(隐含的换行符和参数之间的隐式间距)


如果你想要更多的控制(更多的灵活性,也许?)

格式化输出:是参数之间的间距还是换行符

控制,使用Bruno描述的方法。


I 我不确定你是否可以抑制元素之间的间距(尽管如此,我希望能够纠正)。停止使用隐式换行

类似

打印''测试'',''测试''
''this is a test'' (with implicit newline and implicit spacing between parameters)

If you want more control (more flexibility, perhaps?) over the
formatting of the output: be it spacing between parameters or newline
control, use the methods Bruno describes below.

I''m not sure if you can suppress the spacing between elements (would
love to be corrected though); to stop the implicit newline use
something like
print ''testing'', ''testing''


(但是 - 使用领先的逗号,换行符被抑制)


我个人觉得打印方便句子(或写作

''行'')。


认为值得指出这个以防万一,就像我知道的那样,你带来了具有特定输出流的裁剪器的



一切顺利,


Jon。


Bruno Desthuilliers写道:AMaécrit:

(but - with the leading comma, the newline is suppressed)

I personally find that print is convenient for sentences (or writing
''lines'').

Thought it worth pointing this out in case, like some I know, you come
across a cropper with certain output streams.

All the best,

Jon.

Bruno Desthuilliers wrote: A.M a écrit :



我发现打印方法更加灵活。我可以使用print而不是file.write方法吗?
Hi,
I found print much more flexible that write method. Can I use print instead
of file.write method?



f = open(" / path / to / file")
print>> f,这是我的%s消息 %" first"
f.close()

要打印到stderr:

import sys
print>> sys.stderr,oops

FWIW,你在任何地方使用字符串格式化,不仅仅是在打印语句中:

s =some%s和%formating" %(nice,cool)
print s

你也可以使用dict formating:

names = {" other ;:AM,me, :" bruno"}
s =" hello%(other)s,my name is%(me)s" %name



f = open("/path/to/file")
print >> f, "this is my %s message" % "first"
f.close()

To print to stderr:

import sys
print >> sys.stderr, "oops"

FWIW, you and use string formating anywhere, not only in print statements:

s = "some %s and % formating" % ("nice", "cool")
print s

You can also use "dict formating":

names = {"other": "A.M.", "me" : "bruno"}
s = "hello %(other)s, my name is %(me)s" % names






我的意思是'尾随'':不领先。


mea culpa 。


Jon。


Jon Clements写道:
I meant ''trailing'': not leading.

mea culpa.

Jon.

Jon Clements wrote:
我不知道>>语法:很高兴了解它Bruno - 谢谢
你。

对于OP - 我发现print语句对于以下内容非常有用:
print''the'',' 'is'','a'',''test''
Didn''t know of the >> syntax: lovely to know about it Bruno - thank
you.

To the OP - I find the print statement useful for something like:
print ''this'',''is'',''a'',''test''
''这是一个测试''(带有隐式换行符和参数之间的隐式间距)

如果你想要更多的控制(更多的灵活性,也许?)输出的
格式:无论是参数之间的间距还是新行
控件,请使用下面Bruno描述的方法。

我不确定你是否可以抑制元素之间的间距(尽管如此,爱会被纠正);停止隐式换行使用
打印''测试'',''测试''
''this is a test'' (with implicit newline and implicit spacing between parameters)

If you want more control (more flexibility, perhaps?) over the
formatting of the output: be it spacing between parameters or newline
control, use the methods Bruno describes below.

I''m not sure if you can suppress the spacing between elements (would
love to be corrected though); to stop the implicit newline use
something like
print ''testing'', ''testing''


(但是 - 用前面的逗号,换行符被压制了)

我个人觉得打印方便句子(或写'
''''')。

认为值得指出这个以防万一就像我所知道的那样,你来到一个有特定输出流的裁剪器上。

***的,
Jon。

Bruno Desthuilliers写道:


(but - with the leading comma, the newline is suppressed)

I personally find that print is convenient for sentences (or writing
''lines'').

Thought it worth pointing this out in case, like some I know, you come
across a cropper with certain output streams.

All the best,

Jon.

Bruno Desthuilliers wrote:

AMaécrit:


我发现打印方法更加灵活。我可以使用print而不是file.write方法吗?
Hi,
I found print much more flexible that write method. Can I use print instead
of file.write method?



f = open(" / path / to / file")
print>> f,这是我的%s消息 %" first"
f.close()

要打印到stderr:

import sys
print>> sys.stderr,oops

FWIW,你在任何地方使用字符串格式化,不仅仅是在打印语句中:

s =some%s和%formating" %(nice,cool)
print s

你也可以使用dict formating:

names = {" other ;:AM,me, :" bruno"}
s =" hello%(other)s,my name is%(me)s" %names



f = open("/path/to/file")
print >> f, "this is my %s message" % "first"
f.close()

To print to stderr:

import sys
print >> sys.stderr, "oops"

FWIW, you and use string formating anywhere, not only in print statements:

s = "some %s and % formating" % ("nice", "cool")
print s

You can also use "dict formating":

names = {"other": "A.M.", "me" : "bruno"}
s = "hello %(other)s, my name is %(me)s" % names