Joh*_*son 351 python printing stdout
有哪些sys.stdout.write()情况比较好print?
(示例:更好的性能;更有意义的代码)
luc*_*luc 275
print只是一个瘦的包装器,它格式化输入(可修改,但默认情况下在结尾处有args和换行符之间的空格)并调用给定对象的write函数.默认情况下,此对象是sys.stdout,但您可以使用"chevron"表单传递文件.例如:
print >> open('file.txt', 'w'), 'Hello', 'World', 2+3
Run Code Online (Sandbox Code Playgroud)
请参阅:https://docs.python.org/2/reference/simple_stmts.html?highlight = print#the-print-statement
在Python 3.x中,print成为一个函数,但仍然可以通过其他东西而不是sys.stdout感谢file参数.
print('Hello', 'World', 2+3, file=open('file.txt', 'w'))
Run Code Online (Sandbox Code Playgroud)
请参阅https://docs.python.org/3/library/functions.html#print
在Python 2.6+中,print仍然是一个声明,但它可以用作函数
from __future__ import print_function
Run Code Online (Sandbox Code Playgroud)
更新:Bakuriu在评论中指出的print函数和print语句(更常见的是函数和语句之间)之间有一点区别.
如果在评估参数时出错:
print "something", 1/0, "other" #prints only something because 1/0 raise an Exception
print("something", 1/0, "other") #doesn't print anything. The function is not called
Run Code Online (Sandbox Code Playgroud)
dog*_*ane 139
print首先将对象转换为字符串(如果它不是字符串).如果它不是行的开头和结尾处的换行符,它也会在对象之前放置一个空格.
使用时stdout,您需要自己将对象转换为字符串(例如,通过调用"str"),并且没有换行符.
所以
print 99
Run Code Online (Sandbox Code Playgroud)
相当于:
import sys
sys.stdout.write(str(99) + '\n')
Run Code Online (Sandbox Code Playgroud)
小智 40
我的问题是,是否有
sys.stdout.write()更好的 情况
在前几天完成开发脚本之后,我将其上传到unix服务器.我的所有调试消息都使用了print语句,这些语句不会出现在服务器日志中.
这是您可能需要的情况sys.stdout.write.
Wil*_*nes 36
以下是一些基于Mark Lutz 学习Python的书的示例代码,它解决了您的问题:
import sys
temp = sys.stdout # store original stdout object for later
sys.stdout = open('log.txt', 'w') # redirect all prints to this log file
print("testing123") # nothing appears at interactive prompt
print("another line") # again nothing appears. it's written to log file instead
sys.stdout.close() # ordinary file object
sys.stdout = temp # restore print commands to interactive prompt
print("back to normal") # this shows up in the interactive prompt
Run Code Online (Sandbox Code Playgroud)
在文本编辑器中打开log.txt将显示以下内容:
testing123
another line
Run Code Online (Sandbox Code Playgroud)
Car*_*los 12
至少有一种情况需要你sys.stdout而不是打印.
如果要在不进入下一行的情况下覆盖一行,例如在绘制进度条或状态消息时,需要循环覆盖
Note carriage return-> "\rMy Status Message: %s" % progress
Run Code Online (Sandbox Code Playgroud)
由于print增加了换行符,因此最好使用sys.stdout.
我的问题是,是否有
sys.stdout.write()更好的情况
如果您正在编写可以写入文件和stdout的命令行应用程序,那么它很方便.你可以这样做:
def myfunc(outfile=None):
if outfile is None:
out = sys.stdout
else:
out = open(outfile, 'w')
try:
# do some stuff
out.write(mytext + '\n')
# ...
finally:
if outfile is not None:
out.close()
Run Code Online (Sandbox Code Playgroud)
它确实意味着你不能使用这种with open(outfile, 'w') as out:模式,但有时它是值得的.
在 2.x 中,该print语句预处理您提供的内容,一路将其转换为字符串,处理分隔符和换行符,并允许重定向到文件。3.x 把它变成了一个函数,但它仍然具有相同的职责。
sys.stdout 是一个文件或类似文件,它具有写入它的方法,这些方法采用字符串或沿着该行的东西。
当动态打印有用时,例如在较长的过程中提供信息,这是更好的选择:
import time, sys
Iterations = 555
for k in range(Iterations+1):
# Some code to execute here ...
percentage = k / Iterations
time_msg = "\rRunning Progress at {0:.2%} ".format(percentage)
sys.stdout.write(time_msg)
sys.stdout.flush()
time.sleep(0.01)
Run Code Online (Sandbox Code Playgroud)
小智 5
printPython 3 中需要指出的和之间的区别sys.stdout.write还在于在终端中执行时返回的值。在 Python 3 中,sys.stdout.write返回字符串的长度,而print仅返回None.
例如,在终端中交互运行以下代码将打印出字符串及其长度,因为交互运行时会返回并输出长度:
>>> sys.stdout.write(" hi ")
hi 4
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
543650 次 |
| 最近记录: |