Exemplo n.º 1
0
def write_records(records, properties=None):
    """This method takes a list of named tuple records and a properties file and
    produces an output file containing the records. The properties that are necessary
    are: field_names, and either field_start_positions or a field_separator.
    Optionally, an output file should be specified.
    """
    try:
        file_name = properties['output_file'][0]
    except:
        file_name = get_name(records[0]) + '_' + get_timestamp()
    try:
        fields = properties['field_names']
    except:
        fields = records[0]._fields
    try:
        fields = zip(fields, properties['field_start_positions'],
                     properties['field_end_positions'], properties['field_justify'])
        write_fixed_length_strings(file_name, records, fields)
    except:
        print 'Trying to write records as delimited strings.'
        delimiter = translate_delimiter(properties['field_separator'][0])
        print delimiter, ' (delimiter)'
        write_delimited_strings(file_name, delimiter, records, fields)
    return True