Skip to main content

File writer/parser for fixed-width and character-separated files

Project description

The gocept.recordserialize distribution

File writer/parser for fixed-width and character-separated files.

This package is compatible with Python version 2.7.

Fixed-width format

Define the format by listing all fields, in order. Each field has the following settings:

name:

Name of the field (required)

width:

Width of the field in characters (required)

fill character:

default: Space

alignment:

default: align LEFT (that is, pad on the right side)

For example:

>>> from gocept.recordserialize import FixedWidthRecord
>>> class FixedExample(FixedWidthRecord):
...
...    encoding = 'utf-8'
...    lineterminator = '\r\n'
...
...    fields = [
...        ('one', 3, ' ', FixedWidthRecord.LEFT),
...        ('two', 7, '0', FixedWidthRecord.RIGHT),
...    ]

Writing

>>> r = FixedExample()
>>> r['one'] = 'foo'
>>> r['two'] = '12'
>>> str(r)
'foo0000012\r\n'

Reading

>>> r = FixedExample.parse('bar0000034\r\n')
>>> r['one']
u'bar'
>>> r['two']
u'34'

Also available: parse_file which takes a file-like object.

Character-separated format

Define the format by declaring attributes on the class. Each field has the following settings:

position:

position of the field

default:

default value to write if none is given

maximum length:

truncate field to this length

Note that you do not have to declare all fields, any positions not declared are filled with empty columns. For this reason, you always have to give the total number of fields in the fields attribute:

>>> from gocept.recordserialize import SeparatedRecord
>>> class PipeExample(SeparatedRecord):
...
...    fields = 5
...    encoding = 'utf-8'
...    separator = '|'
...    lineterminator = '\r\n'
...
...    first = 1
...    default = 2, 'qux'
...    maxlen = 3, 3
...    maxlen_default = 4, 5, 'asdfg'

Writing

>>> r = PipeExample()
>>> r['first'] = 'some text'
>>> r['maxlen'] = '12345'
>>> str(r)
'some text|qux|123|asdfg|\r\n'

Reading

>>> r = PipeExample.parse('some text|qux|123|asdfg|\r\n')
>>> r['first']
u'some text'
>>> r['default']
u'qux'
>>> r['maxlen']
u'123'
>>> r['maxlen_default']
u'asdfg'

Also available: parse_file which takes a file-like object.

Escaping

For subclassing, SeparatedRecord provides classmethods escape and unescape that each value is passed through on write/read. An example that makes use of this is gocept.recordserialize.CSVRecord which escapes quotes:

>>> from gocept.recordserialize import CSVRecord
>>> class CSVExample(CSVRecord):
...
...     fields = 1
...     one = 1

>>> r = CSVExample()
>>> r['one'] = 'my "quoted" string'
>>> str(r)
'"my \'quoted\' string"\r\n'

Developing gocept.recordserialize

Author:

gocept <mail@gocept.com>

PyPI page:

https://pypi-hypernode.com/project/gocept.recordserialize/

Issue tracker:

https://bitbucket.org/gocept/gocept.recordserialize/issues

Source code:

https://bitbucket.org/gocept/gocept.recordserialize/

Change log for gocept.recordserialize

1.0 (2017-12-06)

  • Add support for setuptools >= 32.

  • Drop support for Python 2.6.

0.3 (2016-02-29)

  • Escape unicode characters on errors.

0.2 (2013-11-26)

  • Fixed dependency on setuptools.

0.1 (2012-09-19)

initial release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gocept.recordserialize-1.0.tar.gz (11.0 kB view details)

Uploaded Source

File details

Details for the file gocept.recordserialize-1.0.tar.gz.

File metadata

File hashes

Hashes for gocept.recordserialize-1.0.tar.gz
Algorithm Hash digest
SHA256 ef66a416ec4d04aa55e89e78ccbed33749d04a1608ef5174910f14a3dc1c96f1
MD5 de7f5843d23fa1fa97a213f053957ec2
BLAKE2b-256 642ba7a987244271af8eb69f20880369189a2a3045a64c6685ed4f39948c38c9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page