กล่าวอีกนัยหนึ่งว่า sprintf เทียบเท่ากับ pprint คืออะไร
กล่าวอีกนัยหนึ่งว่า sprintf เทียบเท่ากับ pprint คืออะไร
คำตอบ:
pprintโมดูลมีคำสั่งตั้งชื่อpformat , เพียงวัตถุประสงค์ที่
จากเอกสาร:
ส่งคืนการจัดรูปแบบของวัตถุเป็นสตริง เยื้องความกว้างและความลึกจะถูกส่งผ่านไปยังตัวสร้าง PrettyPrinter เป็นพารามิเตอร์การจัดรูปแบบ
ตัวอย่าง:
>>> import pprint
>>> people = [
... {"first": "Brian", "last": "Kernighan"},
... {"first": "Dennis", "last": "Richie"},
... ]
>>> pprint.pformat(people, indent=4)
"[ { 'first': 'Brian', 'last': 'Kernighan'},\n { 'first': 'Dennis', 'last': 'Richie'}]"
สมมติว่าคุณทำสิ่งที่ผิดเพี้ยนไปpprintจากไลบรารี่ที่พิมพ์ออกมาแล้วคุณต้องการpprint.pformatวิธีการ
ถ้าคุณหมายถึง printคุณต้องการstr()
>>> import pprint
>>> pprint.pformat({'key1':'val1', 'key2':[1,2]})
"{'key1': 'val1', 'key2': [1, 2]}"
>>>
คุณกำลังมองหาpprint.pformatอะไร?
บางสิ่งเช่นนี้
import pprint, StringIO
s = StringIO.StringIO()
pprint.pprint(some_object, s)
print s.getvalue() # displays the string