Skip to main content

Easy way converting from text, list, or dict to text or list with fixed widths.

The Installation

>>> pip install pyfixedwidths

Examples

>>> from pyfixedwidths import FixedWidthFormatter
>>> text = (
>>> "1,2,3,4\n"
>>> "11,,33,44\n"
>>> )
>>>
>>> listofdict = [
>>>     dict(name="John Doe", age=20, hobby="swim"),
>>>     dict(name="John Smith", age=100, job="teacher"),
>>> ]
>>>
>>> array = [
>>>     [1, None, 3, 4],
>>>     [11, 22, None, 44],
>>> ]
>>>
>>> fw = FixedWidthFormatter()
>>> fw.from_text(text).to_text(padding=1)
>>> #=>
>>>     ("1  , 2 , 3  , 4 \n"
>>>      "11 ,   , 33 , 44\n")
>>>
>>> fw.from_text(text).to_array(padding=1)
>>> #=>
>>>     [["1  "," 2 "," 3  "," 4 "],
>>>      ["11 ","   "," 33 "," 44"],]
>>>
>>> fw = FixedWidthFormatter()
>>> fw.from_array(array).to_text(padding=0)
>>> #=>
>>>     ("1 ,None,3   ,4 \n"
>>>      "11,    ,None,44\n")
>>>
>>> fw.from_array(array).to_array(padding=0)
>>> #=>
>>>     [["1 ","None","3   ","4 "],
>>>      ["11","    ","None","44"],]
>>>
>>> fw = FixedWidthFormatter(schema=schema)
>>> fw.from_dict(listofdict).to_text(padding=0)
>>> #=>
>>>     ("name      ,age,hobby,job    \n"
>>>      "John Doe  ,20 ,swim ,       \n"
>>>      "John Smith,100,     ,teacher\n")
>>>
>>> fw.from_dict(listofdict).to_list(padding=0)
>>> #=>
>>>     [["name      ", "age", "hobby", "job    "],
>>>      ["John Doe  ", "20 ", "swim ", "       "],
>>>      ["John Smith", "100", "     ", "teacher"],]
>>> schema = [
>>>     dict(
>>>         justification="rjust"
>>>     ),
>>>     dict(
>>>         format=":>5s"
>>>     ),
>>>     dict(),
>>>     dict(
>>>         format=":2s"
>>>     )
>>> ]
>>>
>>> fw = FixedWidthFormatter(schema=schema)
>>> fw.from_dict(listofdict).to_text(padding=2)
>>> #=>
>>>     ("      name  ,    age  ,  hobby  ,  job\n"
>>>      "  John Doe  ,     20  ,  swim   ,    \n"
>>>      "John Smith  ,    100  ,         ,  teacher\n")
>>>
>>> fw.from_dict(listofdict, headers=["hobby", "job", "location", "name"]).to_text(padding=1)
>>> #=>
>>>     (   "hobby ,     job , location , name\n"
>>>         " swim ,         ,          , John Doe\n"
>>>         "      , teacher ,          , John Smith\n")

Requirements

  • Python 3

Release files for pyfixedwidths 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyfixedwidths 0.1.0
File Size Uploaded
pyfixedwidths-0.1.0.tar.gz 3.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyfixedwidths 0.1.0
File Interpreter ABI Platform
pyfixedwidths-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 7.2 kB

Release files / pyfixedwidths-0.1.0.tar.gz

Download URL pyfixedwidths-0.1.0.tar.gz
Size 3.7 kB
Tags Source
SHA-256 checksum
How to use checksums
882fa2d391d127d756ef6c1f4d3e525dce9c1c5be30e2abe5d4d7d3a2a494991
BLAKE2b-256 checksum
How to use checksums
d0098d3541f5249152d277343183ca74fc9919a76a55a3971d22ca6fe775e5fb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.6 CPython/3.6.9 Linux/5.8.0-50-generic

Release files / pyfixedwidths-0.1.0-py3-none-any.whl

Download URL pyfixedwidths-0.1.0-py3-none-any.whl
Size 3.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
723c0758f8c667c5b07389d0b4845bd0566af316afea6a364dc5f0bc168eb146
BLAKE2b-256 checksum
How to use checksums
c2fcc7ea4fcfd63e99b80123d99d60dc07aeb1d936db9debb8e5aeb54c77da72
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.6 CPython/3.6.9 Linux/5.8.0-50-generic

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page