簡介
pprint模組提供了列印出python資料結構類和方法。
模組方法:
建立乙個prettyprinter物件
indent --- 縮排,
width --- 一行最大寬度,
depth --- 列印的深度,這個主要是針對一些可遞迴的物件,如果超出指定depth,其餘的用"..."代替。
eg: a=[1,2,[3,4,],5] a的深度就是2; b=[1,2,[3,4,[5,6]],7,8] b的深度就是3
stream ---指輸出流物件,如果stream=none,那麼輸出流物件預設是sys.stdout
返回格式化的物件字串
輸出格式的物件字串到指定的stream,最後以換行符結束。
判斷物件object的字串物件是否可讀
判斷物件是否需要遞迴的表示
eg: pprint.
isrecursive
(a) --->false
pprint.
isrecursive
([1,2,3])
-->true
返回乙個物件字串,物件中的子物件如果是可遞迴的,都被替換成on
typename
with
id=number>
.這種形式。
eg:
[python] view plain
copy
import
pprint
data = (
"this is a string"
, [1, 2
, 3, 4
], (
"more tuples"
, 1.0
, 2.3
, 4.5
), "this is yet another string"
) pprint.pprint(data)
輸出:('this is a string',
[1, 2, 3, 4],
('more tuples', 1.0, 2.3, 4.5),
'this is yet another string')
python之格式化輸出
1 整數的輸出 o oct 八進位制 d dec 十進位制 x hex 十六進製制 2 浮點數輸出 1 格式化輸出 f 保留小數點後面六位有效數字 3f,保留3位小數字 e 保留小數點後面六位有效數字,指數形式輸出 3e,保留3位小數字,使用科學計數法 g 在保證六位有效數字的前提下,使用小數方式,...
Python基礎之格式化輸出
python中用 代 式符,或者叫佔位符 表示格式化操作,將其轉化成相應的資料型別 age 10print 我今年 d歲 age 我今年10歲在程式中,看到了 這樣的操作符,這就是python格式化輸出 age 19name xiaoming print 我的名字是 s,年齡是 d歲 name,ag...
Python 之輸出format格式化
python使用format進行格式化輸出 1 使用槽 的概念進行格式化輸出。print 這是格式化輸出的例項,年月日 format nian,yue,ri 上面有3個槽,編號依次為0,1,2分別對應format中的nian,yue.ri.三個變數。2 擴充套件,對應以上三個變數,我們可以內定改變其...