當前位置:首頁 » 編程語言 » pythonsysstdout

pythonsysstdout

發布時間: 2025-07-14 16:15:29

A. 求python 中sys.stdout.write 和 print >> sys.stdout的區別,越詳細越好

下面應該可以解你的惑了:

print >> sys.stdout的形式就是print的一種默認輸出格式,等於print "%VALUE%"

看下面的代碼的英文注釋,是print的默認幫助信息

#coding=utf-8
importsys,os

list1Display=['1','2','3']
list2Display=['abc','def','rfs']
whilelist2Display!=[]:
#Printsthevaluestoastream,ortosys.stdoutbydefault.
#Optionalkeywordarguments:
#file:afile-likeobject(stream);defaultstothecurrentsys.stdout.
#sep:stringinsertedbetweenvalues,defaultaspace.
#end:,defaultanewline.
#print可以將值輸出到指定的輸出流(可以是文件句柄),若不指定,
#則輸出到stdout(標准輸出)
#一般我們使用的時候不加輸出定向符「>>」到輸出的file對象,本代碼中對象是stdout
#下面的print在stdout對象中每次輸出兩個值
print>>sys.stdout,list2Display.pop(),list1Display.pop()
os.system("pause")

上文中只演示了python2.x中的用法,2.x中的print無法指定end符號為其他值,默認會輸出一個" ",也就是用一次必定換到下一行,到了3.x中print成為了一個真正意義上的函數,後來就可以任意指定end符號的值,你可以輸出一次後末尾添加上任意你想要的值,而不是強制換行。



因此在2.x中若想實現輸出不換行,只能直接調用stdout對象的write方法了,下面也是一個實例,因為stdout沒有end這個符號這一說,輸出不會換行,因此如果你想同一樣輸出多次,在需要輸出的字元串對象裡面加上" ",就可以回到行首了。

#coding=utf-8
importsys,os
importtime
foriinrange(100):
time.sleep(.5)
sys.stdout.write("Filetransferprogress:[%3d]percentcomplete! "%i)
sys.stdout.flush()

體會一下,將上面的" "拿掉試試看,是不是不換行而直接輸出了?明白了么。很長一段時間內python都會停留在2.x的時代。

熱點內容
安卓備忘錄怎麼轉蘋果去 發布:2025-07-14 20:57:16 瀏覽:114
加密貨幣app 發布:2025-07-14 20:46:57 瀏覽:553
醫院防汛演練方案與腳本 發布:2025-07-14 20:41:11 瀏覽:777
公網ip做域伺服器地址 發布:2025-07-14 20:40:13 瀏覽:52
php獲取幾 發布:2025-07-14 20:22:20 瀏覽:376
舊安卓如何刷入最新安卓 發布:2025-07-14 20:16:14 瀏覽:763
伺服器或網路不給力是什麼意思 發布:2025-07-14 20:15:36 瀏覽:318
爬網站資料庫 發布:2025-07-14 20:15:20 瀏覽:520
邵雍的演算法 發布:2025-07-14 20:13:49 瀏覽:119
離線燒錄加密 發布:2025-07-14 20:12:13 瀏覽:620