Creating Fixed Length Records

Jeff Shannon jeff at ccvcorp.com
Thu Dec 9 03:20:53 CET 2004


Greg Lindstrom wrote:

> [...] I concatenate the field to the text segment.  I have been led to 
> believe this is bad form because Python will copy the entire segment 
> each time I add a field.  Up until now, it was not a big deal because 
> the segments had at most 20 fields.  I have just been handed a record 
> layout that is almost 5000 bytes long with 350 fields in it.  Gulp!!  
> Although I could let my object churn away on this bad boy, I'd like to 
> know if there is a more pythonic way to serialize the record.


The standard Python way would be to read your fields into a list, and 
then join() the list.  Psuedocode-ish example:

    values = []
    for field in list_of_fields:
        this_value = database.get(field)  # or however you want to 
retrieve it
        values.append(this_value)
    text = ''.join(values)

Of course, if you want to be able to reverse the process, then you will 
need some kind of format definition.  This could be an explicit list of 
column numbers where fields break, or an implicit list in the form of a 
struct module format string, or whatever.  Using the struct module would 
(I believe) be a reasonable way of getting data into/out of the 
fixed-length block.

Jeff Shannon
Technician/Programmer
Credit International




More information about the Python-list mailing list