python標准輸出
❶ python標准輸出重定向stdout.py的意思
sys.stdout 默認就是輸出到控制台(console),print 默認的輸出也是 sys.stdout,所以輸出到控制台。
在 輸入B 那,做了上下文切換with open
,也就是把默認的輸出流指向到文件 out.log,
對應的代碼是: sys.stdout = self.out_new,這里 out_new -> out.log,out_old = console
所以就print 指向文件,而不是控制台了
離開語句時,執行 sys.stdout = self.out_old => sys.stdout = console,還原原來的默認輸入流
於是後面就輸出到默認的控制
❷ python 數據輸出
產生輸出的最簡單方法是使用print語句,可以通過用逗號分隔零個或多個表達式。這個函數傳遞表達式轉換為一個字元串,如下結果寫到標准輸出-
print("Pythonisreallyagreatlanguage,","isn'tit?")
1
這將產生以下結果標准屏幕上:
Pythonisreallyagreatlanguage,isn'tit?
❸ python怎麼輸出string
字元串靜態拼接
print 'a' 'b'
=> ab
用空格分隔,並在最後追加換行符
print 'a', 'b'
=> a b
用空格分隔,不在最後追加換行符
print 'a', 'b',
=> a b
列印元組 ('a', 'b') 的 str 形式,並在最後追加換行符
print('a', 'b'),
=> ('a', 'b')
和上個等價
print('a', 'b',)
=> ('a', 'b')
列印元組 ('a', 'b') 的 str 形式,不在最後追加換行符
print('a', 'b'),
=> ('a', 'b')
不在最後追加換行符
print('hello'),
=> hello
----------------------------------------
列印到標准錯誤流
import sys
print >> sys.stderr, 'spam'
sys.stderr.write('spam\n')
from __future__ import print_function
print('spam', file = sys.stderr)
----------------------------------------
列印到文件流
logfile = open('/tmp/mylog.txt', 'a')
print >> logfile, 'Fatal error: invalid input!'
logfile.close()
❹ 請教關於用python獲取標准輸出流中內容的問
import sysprint sys.stdin可以用標准輸入讀取吧
❺ 關於python的輸出重定向
importsys
f=open('a.txt','w')
print>>sys.stdout,'hello,world'
hello,world
print>>f,'hello,world'
f.close()
輸出到屏幕的內容重定向到文件,供參考。
defprint(stream):
"""print(value,...,sep='',end='\n',file=sys.stdout)
Printsthevaluestoastream,ortosys.stdoutbydefault.
Optionalkeywordarguments:
file:afile-likeobject(stream);defaultstothecurrentsys.stdout.
sep:stringinsertedbetweenvalues,defaultaspace.
end:,defaultanewline."""
pass
❻ 請教關於用python獲取標准輸出流中內容的問題
importsys
printsys.stdin
可以用標准輸入讀取吧
❼ Python輸出!
print sum([x * (x - 1) for x in [y * y for y in xrange(3,11)]])
表達式解析
for y in xrange(3,11)
for y in xrange(3,11) 表示y從3取到10 相當於[3,4,5,6,7,8,9,10]
[y * y for y in xrange(3,11)] 表示生成一個list,其中每個元素都是y*y,由於y是從3-10的,故該表達式最終生成[9,16,25,36,49,64,81,100]這樣一個list
for x in [y * y for y in xrange(3,11)]]就相當於 x從[9,16,25,36,49,64,81,100]中依次取值
[x * (x - 1) for x in [y * y for y in xrange(3,11)]]就相當月[9*8,16*15,25*24,36*35,49*48,64*63,81*80,100*99]
最後sum([x * (x - 1) for x in [y * y for y in xrange(3,11)]])表示對元素求和,
print 向標准輸出,列印上述表達式結果,最終應該等於24936
y y x x-1 x*x-1
3 3 9 8 72
4 4 16 15 240
5 5 25 24 600
6 6 36 35 1260
7 7 49 48 2352
8 8 64 63 4032
9 9 81 80 6480
10 10 100 99 9900
求和結果:24936
❽ python編寫程序對標准輸入的10個整數置逆序排列並輸出
lin1=[]
foriinrange(1,11):
lin1.append(i)
lin1.reverse()
print(lin1)
❾ Python我想輸出一個標準的x字母用符號輸出,為啥老是這樣
兩種款式都可以
❿ 如何用Python調用一個外部可執行文件,並獲得其標准輸出
os.popen或subprocess.popen