https://docs.python.org/3/tutorial/stdlib2.html#output-formatting
출력 형식 관련 표준 라이브러리 모듈
reprlib
- 기본 repr()보다 큰/중첩 컨테이너를 간결하게 잘라 보여 줌
import reprlib
print(reprlib.repr(set('supercalifragilisticexpialidocious')))
# 출력: {'a', 'c', 'd', 'e', 'f', 'g', ...}pprint
- 내장·사용자 객체를 해석하기 쉬운 형태로 정렬
- 결과가 길면 자동으로 줄바꿈·들여쓰기를 추가해 구조 파악이 쉬움
import pprint
t = [[[['black', 'cyan'], 'white', ['green', 'red']],
[['magenta', 'yellow'], 'blue']]]
pprint.pprint(t, width=30)
# [[[['black', 'cyan'],
# 'white',
# ['green', 'red']],
# [['magenta', 'yellow'],
# 'blue']]]textwrap
- 긴 문자열을 지정 폭에 맞춰 줄바꿈/들여쓰기
- 콘솔 도움말, 이메일 본문 등에서 균일한 폭 유지
import textwrap
doc = """The wrap() method is just like fill() except that it returns a list of strings instead of one big string with newlines to separate the wrapped lines."""
print(textwrap.fill(doc, width=40)) #40자 폭 맞춘 문단
# The wrap() method is just like fill()
# except that it returns a list of strings
# instead of one big string with newlines
# to separate the wrapped lines.locale
- 문화권별 숫자·통화·날짜 형식 제공
import locale
locale.locale_alia # 별칭 확인
locale.setlocale(locale.LC_ALL, 'en_US.ISO8859-1') #locale 세팅
conv = locale.localeconv() # 세팅 확인
x = 1234567.8
# 미국식 천 단위 구분
print(locale.format_string("%d", x, grouping=True))
# 미국 달러 형식
print(locale.format_string("%s%.*f",(conv['currency_symbol'], conv['frac_digits'], x),grouping=True))