Python Humanize Utilities
humanize is a Python library (current version 4.15.0) that provides a collection of utilities to make data more human-readable. It transforms raw numerical values, dates, times, and file sizes into intuitive and easily understandable formats, bridging the gap between machine-centric data and human comprehension. The project maintains an active release cadence, with frequent updates and a focus on expanding localization support.
Warnings
- breaking Python 3.9 support was dropped in humanize version 4.14.0. Users running Python 3.9 or older environments must either upgrade their Python version or pin humanize to a version less than 4.14.0.
- breaking Python 3.8 support was dropped in humanize version 4.11.0. Users running Python 3.8 or older environments must either upgrade their Python version or pin humanize to a version less than 4.11.0.
- gotcha `naturaldelta` in versions prior to 4.13.0 had rounding issues that could lead to less precise human-readable duration strings.
- gotcha Versions 4.12.0 and 4.12.1 introduced regressions where `naturalsize` could incorrectly format float inputs.
Install
-
pip install humanize
Imports
- humanize
import humanize
- intcomma
from humanize import intcomma
- naturalsize
from humanize import naturalsize
Quickstart
import humanize
import datetime as dt
# Humanize numbers
print(f"Commas: {humanize.intcomma(1234567)}")
print(f"Words: {humanize.intword(1234567890)}")
print(f"Ordinal: {humanize.ordinal(21)}")
# Humanize dates and times
now = dt.datetime.now()
one_hour_ago = now - dt.timedelta(hours=1, minutes=5)
tomorrow = now + dt.timedelta(days=1)
print(f"Time ago: {humanize.naturaltime(one_hour_ago)}")
print(f"Time from now: {humanize.naturaltime(tomorrow, future=True)}")
print(f"Natural day: {humanize.naturalday(now)}")
# Humanize file sizes
print(f"File size: {humanize.naturalsize(1024 * 1024 * 1.5)}")