다시 말해, 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'}]"
답변
pretty-print librarypprint
에서 실제로 의미한다고 가정하면 방법 을 원합니다 .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
답변
