Parsing Fixed-Length File Records with Python
Parsing Fixed-Length File Records with Python
Join the DZone community and get the full member experience.
Join For FreeLearn how Crafter’s Git-based content management system is reinventing modern digital experiences. Download this white paper now.
>>> from struct import unpack >>> from collections import namedtuple >>> line = ' 23C 17000' >>> Transaction = namedtuple('Transaction', 'code status amount') >>> format = '<6sc8s' # code: 6bytes, status: 1byte, amount: 8bytes >>> txn = Transaction._make(unpack(format, line)) >>> txn Transaction(code=' 23', status='C', amount=' 17000') >>> txn.code, txn.status, txn.amount (' 23', 'C', ' 17000')
Crafter CMS is a modern Git-based platform for building innovative websites and content-rich digital experiences. Download this white paper now.
Published at DZone with permission of Ruslan Spivak , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ parent.urlSource.name }}