Mixed Width
About
This project is designed to enable the easy reading/writing of fixed-width files with variable widths. For example:
FOO BAR BAZ
Hello World SomeExtraLongString
In this case we have differing widths between the columns. This means we can't just specify a single width for the columns. This might not be much of an issue itself, just code in the differing widths and parse using that, however with things like console output we can't rely on a consistent width for cells, which is where Mixed Width comes in. To resolve this, it detects the start of cells by looking for location where spaces precede letters which line up across all lines of the files. Using this method we are able to parse files with unknown widths and convert them into either a list of lists, or a list of dictionaries where the keys are the header values.
Examples
To use multiwidth for the most part it's the same as the built-in json module. This means that parsing some output takes the form of:
import multiwidth
string_to_parse = """FOO BAR BAZ
Hello World SomeExtraLongString"""
data = multiwidth.loads(string_to_parse)
print(data)
# output:
# [['Hello', 'World', 'SomeExtraLongString']]
If preserving the headers is important, output_json=True can be added to the loads method:
import multiwidth
string_to_parse = """FOO BAR BAZ
Hello World SomeExtraLongString"""
data = multiwidth.loads(string_to_parse, output_json=True)
print(data)
# output:
# [{'FOO': 'Hello', 'BAR': 'World', 'BAZ': 'SomeExtraLongString'}]
Each line will then be a dictionary with the header keys and their corresponding values
In addition, if the content is stored in a file, multiwidth.load(<file_object>) can be used.
Finally, data can be output as well from multiwidth.
import multiwidth
headers = ['FOO', 'BAR', 'BAZ']
data = [['Hello', 'World', 'SomeExtraLongString']]
print(multiwidth.dumps(data, headers=headers))
# Output:
# FOO BAR BAZ
# Hello World SomeExtraLongString
You can also control the spacing between columns with cell_suffix='<your desired padding between columns>'. For example:
import multiwidth
headers = ['FOO', 'BAR', 'BAZ']
data = [['Hello', 'World', 'SomeExtraLongString']]
print(multiwidth.dumps(data, headers=headers, cell_suffix=' '))
# Output:
# FOO BAR BAZ
# Hello World SomeExtraLongString
You can also dump JSON data by omitting the headers argument:
import multiwidth
data = [{'FOO': 'Hello', 'BAR': 'World', 'BAZ': 'SomeExtraLongString'}]
print(multiwidth.dumps(data))
# Output:
# FOO BAR BAZ
# Hello World SomeExtraLongString
Finally, you can dump to a file with dumps(<your file object>)
Usage
load
"""Parse data from a file object
Args:
file_object (io.TextIOWrapper): File object to read from
padding (str, optional): Which character takes up the space to create the fixed
width. Defaults to " ".
header (bool, optional): Does the file contain a header. Defaults to True.
output_json (bool, optional): Should a list of dictionaries be returned instead
of a list of lists. Defaults to False. Requires that 'header' be set to
True.
Returns:
Union[List[List],List[Dict]]: Either a list of lists or a list of dictionaries that
represent the extracted data
"""
loads
"""Takes a string of a fixed-width file and breaks it apart into the data contained.
Args:
contents (str): String fixed-width contents.
padding (str, optional): Which character takes up the space to create the fixed
width. Defaults to " ".
header (bool, optional): Does the file contain a header. Defaults to True.
output_json (bool, optional): Should a list of dictionaries be returned instead
of a list of lists. Defaults to False. Requires that 'header' be set to
True.
Raises:
Exception: 'output_json' is True but 'header' is False.
Returns:
List[List] | List[Dict]: Either a list of lists or a list of dictionaries that
represent the extracted data
"""
dump
"""Dumps a formatted table to a file
Args:
data (Union[List[List],List[Dict]]): Data to dump to a file. If using JSON data
then omit the `headers` argument
file_object (io.TextIOWrapper): File object to write to
headers (List[str], optional): Headers to use with list data. Defaults to None.
padding (str, optional): Character to use as padding between values. Defaults to
' '.
cell_suffix (str, optional): String to use as the padding between columns.
Defaults to ' '.
"""
dumps
"""Dumps a formatted table to a string
Args:
data (Union[List[List],List[Dict]]): List or dictionary data to format
headers (List[str], optional): Headers to use with list data. Defaults to None.
padding (str, optional): Character to use as padding between values. Defaults to
' '.
cell_suffix (str, optional): String to use as the padding between columns.
Defaults to ' '.
Returns:
str: Formatted table of input data
"""
License
Multiwidth is under the MIT license.
Contact
If you have any questions or concerns please reach out to me (John Carter) at jfcarter2358@gmail.com
Metadata
Release files for multiwidth 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| multiwidth-1.0.1.tar.gz | 6.2 kB | Without provenance | Details for multiwidth-1.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| multiwidth-1.0.1-py3-none-any.whl | Python 3 | none | any | Details for multiwidth-1.0.1-py3-none-any.whl |
Total release size: 12.8 kB
Release files / multiwidth-1.0.1.tar.gz
| Download URL | multiwidth-1.0.1.tar.gz |
|---|---|
| Size | 6.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f782ea465712dd9f2dcd576ade3bf7be905b3becd648ba9c05d33191d04dac89
|
|
BLAKE2b-256 checksum How to use checksums |
859754f5c24c9d9c2fed21ec4d6b2396a3a72c504a2e4ec10fe204930dcedb33
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.3.1 CPython/3.9.7 Linux/5.15.83.1-microsoft-standard-WSL2
|
Release files / multiwidth-1.0.1-py3-none-any.whl
| Download URL | multiwidth-1.0.1-py3-none-any.whl |
|---|---|
| Size | 6.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
842d13fac268abf7ab90c5daad45d07d4ef6c5c86a17f88798b3c2a5fa6124b1
|
|
BLAKE2b-256 checksum How to use checksums |
6485e9fa9fbf2ea37c9b0839ca71e09505241d3dbc173defea2fc1f0dca7428b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.3.1 CPython/3.9.7 Linux/5.15.83.1-microsoft-standard-WSL2
|
Security for multiwidth 1.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.