Learning Python Part II 之 Print

pass

Python3.X的print函數

嚴格來講,printing在Python3.X中並不是一個單獨的語句,而是之前所說的表達式語句。
print內置函數通常單獨一行調用,因爲它不返回任何值(嚴格來講是返回None)。但因爲它是函數,我們使用標準的函數調用語法而不是特殊的語句格式。並且因爲它通過關鍵詞參數提供特殊的操作模式,所以這種形式更通用和以後的拓展。

調用格式

print([object, ...][, sep=' '][, end='\n'][, file=sys.stdout][, flush=False])
  • sep是插入在每個對象之間的字符串,默認是空格;傳入空字符串會將所有對象壓縮到一起。
  • end是追加到打印字符串末尾的字符串,默認是換行符 \n ;傳入空字符串會取消換行
  • file指定要將字符串打印到目標文件,標準流或類文件對象。默認是sys.stdout標準輸出流
  • flush,在3.3時添加,默認False;它爲True時會強制把緩存區的內容寫入到待接收文件中。

舉例

>>> print()  #打印空行
>>> x = 'spam'
>>> y = 99
>>> z = ['eggs']
>>> print(x,y,z)
spam 99 ['eggs']
>>> print(x,y,z, sep='')   #文本間插入字符
spam99['eggs']
>>> print(x, y, z, sep= ',')
spam,99,['eggs']
>>> 
>>> print(x, y, z, end='')   #結尾追加字符
spam 99 ['eggs']>>> 
>>> print(x, y, z, end=''); print(x, y, z)
spam 99 ['eggs']spam 99 ['eggs']
>>> print(x, y, z, end='...\n')
spam 99 ['eggs']...
>>> print(x, y, z, sep='...', end='!\n')
spam...99...['eggs']!
>>> print(x, y, z, end='!\n', sep='...')
spam...99...['eggs']!
>>> print(x, y, z, sep='...', file=open('data.txt', 'w')) #向文件中輸出字符
>>> print(x, y, z)
spam 99 ['eggs']
>>> print(open('data.txt').read())
spam...99...['eggs']

最後銘記這幾種參數只是提供了方便,如果你需要特定的格式,請提前使用字符串格式化。例如:

>>> text = '%s: %-.4f, %05d' % ('Result', 3.14159, 42)
>>> print(text)
Result: 3.1416, 00042
>>> print('%s: %-.4f, %05d' % ('Result', 3.14159, 42))
Result: 3.1416, 00042

Python2.X的print語句

在2.x中printing使用獨特的語法而不是調用內建函數

語句格式

這裏寫圖片描述

舉例

>>> x = 'a'
>>> y = 'b'
>>> print x, y
a b
>>> print x, y,; print x, y
a b a b
>>> print x + y
ab
>>> print '%s...%s' % (x, y)
a...b

print流重定向

Python中的’hello world’

>>> print('hello world')  #3.x
hello world
>>> print 'hello world'   #2.x
hello world
>>> 'hello world'   #直接輸出
'hello world'

這幾個程序一點也不神祕,但卻能完成打印操作。事實上,print操作只是Python中衆多人體工學特性的一角——它提供了sys,stdout對象的簡便接口,並且帶有一些默認格式。如果你想更麻煩一點也可以用這種方法:

>>> import sys
>>> sys.stdout.write('hello world\n')
hello world

流重定向

 print(X, Y)

import sys
sys.stdout.write(str(X) + ' ' + str(Y) + '\n')

是等價的,後者能幫助我們理解print操作是如何做的,在理解了之後我們可以重定向sys.stdout到區別於標準輸出流的不同的地方。換句話說,這提供了一種方法print操作把文本送到其他地方,例如:

import sys
sys.stdout = open('log.txt', 'a') #重定向到一個文件
...
print(x, y, x)

重定向之後,print之後所有的操作都會將內容輸出到log文本中。print會調用sys.stdoutwrite方法,而sys.stdout是指向文件對象的。

但這會遇到一個問題,當重定向之後,會失去的流方向,所以我們可以提前儲存以便以後恢復:

>>> import sys
>>> temp = sys.stdout
>>> sys.stdout = open('log.txt', 'a')
>>> print('spam')
>>> print(1, 2, 3)
>>> sys.stdout.close()
>>> sys.stdout = temp
>>> print('back here')
back here
>>> print(open('log.txt').read())
spam
1 2 3

在Python中print也提供了一種方法臨時重定向sys.stdout,普通的print會直接切換到原始的輸出流。例如:

#3.X
log = open('log.txt', 'a')
print(x, y, z, file=log)
print(a, b, c)
#2.X
log = open('log.txt', 'a')
print >> log, x, y, z
print a, b, c

這種方法很方便但要確定傳遞的是文件對象。
這種拓展形式的print也用來輸出error信息到error

>>> import sys
>>> sys.stderr.write(('Bad!' * 8) + '\n')
Bad!Bad!Bad!Bad!Bad!Bad!Bad!Bad!
>>> print('Bad!' * 8, file=sys.stderr)
Bad!Bad!Bad!Bad!Bad!Bad!Bad!Bad!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章