Pandas extension for COBOL-style fixed-width files handling multibyte encoding.
Project description
pandas-cobol-io
Pandas extension for COBOL-style fixed-width files (FWF).
Installation
pip install pandas-cobol-io
Usage
To import df.to_fwf and pd.parse_fwf
import pandas as pd
import pandas_cobol_io
When user imports pandas_cobol_io, these two functions are added to pandas objects.
df.to_fwf (DataFrame -> Fixed Width Format Text)
to_fwf method is added to DataFrame.
df = pd.DataFrame({
"ID": [1, 20],
"NAME": ["A", "B"],
})
# Metadata: List of (Type, Length)
# "9": Zero padding (Right aligned)
# "X": Space padding (Left aligned)
metadata = [
("9", 5),
("X", 10),
]
# Convert to fixed-width strings
lines = df.to_fwf(metadata, enc="cp932")
# Save to file
with open("output.txt", "w", encoding="cp932") as f:
f.writelines(lines)
# output.txt
# 00001A # <- White spaces exist here.
# 00020B # <-
# 123456789111111
# 012345
pd.parse_fwf (Fixed Width Format Text -> DataFrame)
parse_fwf function is added to pd.
params = {
"ID": 5,
"NAME": 10,
}
result = pd.parse_fwf("output.txt", params, enc="cp932")
df, errors, lines = (result[x] for x in ("df", "errors", "lines"))
print(df)
# ID NAME
# 0 00001 A
# 1 00020 B
miscellaneous
from pprint import pprint
from pandas_cobol_io import Fwf, fwf_row
fwf = Fwf(enc := "cp932")
# header
header_info = {
"data-distinction": (1, "9", 2),
"dummy": (" ", "X", 13),
}
fwf.header = fwf_row(header_info, enc)
# contents
df = pd.DataFrame({
"ID": [1, 20],
"NAME": ["A", "B"],
})
metadata = [
("9", 5),
("X", 10),
]
fwf.contents = df.to_fwf(metadata, enc)
# footer
tracker_info = {
"data-distinction": (9, "9", 2),
"dummy": (" ", "X", 13),
}
end_info = {
"dummy": ("EOF", "X", 15),
}
fwf.footer = [fwf_row(x, enc) for x in (tracker_info, end_info)]
pprint(fwf)
pprint(fwf.data)
# Fwf(encoding='cp932',
# header=['01 \n'],
# contents=['00001A \n', '00020B \n'],
# footer=[['09 \n'], ['EOF \n']])
# ['01 \n',
# '00001A \n',
# '00020B \n',
# '09 \n',
# 'EOF \n']
License
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pandas_cobol_io-0.1.0.tar.gz.
File metadata
- Download URL: pandas_cobol_io-0.1.0.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
49e9fcbbb538d4c3a64b04469ccc287d239940f2aa748cef0665fe051c25cb1f
|
|
| MD5 |
a5f67843c3d2d62c2918e4dbae3b9a58
|
|
| BLAKE2b-256 |
2836ed41e2d156d92794688debf7ec178c308b49b30bb18ee6526f47e945b552
|
File details
Details for the file pandas_cobol_io-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pandas_cobol_io-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d6d0a80ecf902bae25e810c197e2f5d0c1301ae10c966effb83a28521fcbc07d
|
|
| MD5 |
2298198916d87706ccc7fb5dd3460ed9
|
|
| BLAKE2b-256 |
347d8f51e62347e38fbf399875500ddb78a5ac55d51253e240f5989c46b2ccbe
|