Working with fixed format text db's

mensanator at aol.com mensanator at aol.com
Sat Jun 9 01:22:38 EDT 2007


On Jun 8, 6:18?pm, Ben Finney <bignose+hates-s... at benfinney.id.au>
wrote:
> Neil Cerutti <horp... at yahoo.com> writes:
> > I was hoping for a module that provides a way for me to specify a
> > fixed file format, along with some sort of interface for writing and
> > reading files that are in said format.
>
> Isn't that done by the 'struct' module
> <URL:http://www.python.org/doc/lib/module-struct>?
>
>     >>> records = [
>     ...     "Foo     13 Bar     ",
>     ...     "Spam    23 Eggs    ",
>     ...     "Guido   666Robot   ",
>     ... ]
>     >>> record_format = "8s3s8s"
>     >>> for record in [struct.unpack(record_format, r) for r in records]:
>     ...     print record
>     ...
>     ('Foo     ', '13 ', 'Bar     ')
>     ('Spam    ', '23 ', 'Eggs    ')
>     ('Guido   ', '666', 'Robot   ')

But when you pack a struct, the padding is null bytes,
not spaces.


>
> --
>  \     "Buy not what you want, but what you need; what you do not need |
>   `\           is expensive at a penny."  -- Cato, 234-149 BC, Relique |
> _o__)                                                                  |
> Ben Finney





More information about the Python-list mailing list