程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Detailed explanation of pprint module in Python -- the difference between print() and pprint()

編輯:Python

pprint English full name Data pretty printer, Just as the name suggests, it makes the display result more beautiful .

print() and pprint() All are python Print module for , The function is basically the same , The only difference is pprint() The data structure printed out by the module is more complete , Each row has a data structure , Easier to read printouts . Especially for very long data printing ,print() The output results are all on one line , Inconvenient to view , and pprint() Use branch printout , Therefore, the data structure is more complex 、 Data with long data length , Suitable for adoption pprint() Printing method . Of course , In general, most of them use print().
Code example :

import pprint
data=['generate_csv\\train_00.csv','generate_csv\\train_01.csv',
'generate_csv\\train_02.csv', 'generate_csv\\train_03.csv',
'generate_csv\\train_04.csv', 'generate_csv\\train_05.csv',
'generate_csv\\train_06.csv', 'generate_csv\\train_07.csv',
'generate_csv\\train_08.csv', 'generate_csv\\train_09.csv',
'generate_csv\\train_10.csv', 'generate_csv\\train_11.csv']
print(data)
print("-------- Demarcation line --------------")
pprint.pprint(data)

Code run results :

This example uses long data with complex data structure , so pprint() The output is more standardized and easy to read .

Extended reading :
pprint There are other methods for the module as follows , See official documents for details :

pprint.pformat((object,indent=1,width=80, depth=None)
# Returns the formatted object string 
pprint.pprint(object,stream=None,indent=1, width=80, depth=None)
# Output the formatted object string to the specified stream, End with a line break 
pprint.isreadable(object)
# Judge the object object Whether the string object of is readable 
pprint.isrecursive(object)​​​​​​​
# Determine whether an object needs recursive representation 
pprint.saferepr(object) ​​​​​​​
# Returns an object string , If the child object in the object is recursive , It's all replaced by . This form 

————————————————
Copyright notice : This paper is about CSDN Blogger 「 Relying on the sword, Tianke 」 The original article of , follow CC 4.0 BY-SA Copyright agreement , For reprint, please attach the original source link and this statement .
Link to the original text :https://blog.csdn.net/menghuanshen/article/details/104236369


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved