Humanize Package in Python
Humanize is a package in python which contains various humanization utilities like turning a number into a human-readable size or throughput or number. In this article, we will discuss how to install this package and what are the different utilities present in this package.
Installation: To install this package, we will use pip a command. Python pip is the package manager for Python packages. pip comes pre-installed on 3.4 or older versions of Python, pip commands are used in the command prompt. The following command is used to install the package:
pip install humanize
Usage: This package offers various utilities which can be used on the numbers to make the numbers easily readable for the humans. The utilities of the package are:
- File Size Utility: This utility can convert large integers of file sizes to human-readable form. The default unit of the size it accepts is bytes. For example:
import humanize
size = humanize.naturalsize( 1024000 )
|
Output:
1.0 MB
- Scientific Notation: This utility is used to add scientific notation to the program. This utility also gives an option to add precision to the number. Precision here means the number of digits needed in the number. For example:
import humanize
gfg = humanize.scientific( 2000 )
print ( 'Without Precision: ' + gfg)
gfg = humanize.scientific( 2 * * 10 , precision = 5 )
print ( 'With Precision: ' + gfg)
|
Output:
Without Precision: 2.00 x 10³
With Precision: 1.02400 x 10³
- Floating Point to Fractions: This utility is used to convert a floating-point to fractions. For example:
import humanize
gfg = humanize.fractional( 0.5269 )
print (gfg)
|
Output:
333/632
- Date & Time Utility: Many a times, we encounter few scenarios where the date or time is returned in the form of numbers. This utility is used to convert the date into a human understandable format. For example:
import humanize
import datetime as dt
gfg = humanize.naturaldate(dt.date( 2020 , 5 , 3 ))
print (gfg)
gfg = humanize.naturaldelta(dt.timedelta(seconds = 900 ))
print (gfg)
|
Output:
May 03 2020
15 minutes
- Integer Utility: This utility is used to make integer values more presentable. For Example:
import humanize
gfg = humanize.intcomma( 14523689 )
print (gfg)
gfg = humanize.intword( 1562345640 )
print (gfg)
gfg = humanize.apnumber( 5 )
print (gfg)
|
Output:
14, 523, 689
1.6 billion
five
References: https://pypi.org/project/humanize/
Last Updated :
23 May, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...