Python faster way to read fixed length fields form a file into dictionary

Posted by Martlark on Stack Overflow See other posts from Stack Overflow or by Martlark
Published on 2010-05-06T05:44:37Z Indexed on 2010/05/06 5:48 UTC
Read the original article Hit count: 264

I have a file of names and addresses as follows (example line)

OSCAR    ,CANNONS      ,8     ,STIEGLITZ CIRCUIT

And I want to read it into a dictionary of name and value. Here self.field_list is a list of the name, length and start point of the fixed fields in the file. What ways are there to speed up this method? (python 2.6)

def line_to_dictionary(self, file_line,rec_num):
  file_line = file_line.lower()  # Make it all lowercase

  return_rec = {}  # Return record as a dictionary

  for (field_start, field_length, field_name) in self.field_list:

    field_data = file_line[field_start:field_start+field_length]

    if (self.strip_fields == True):  # Strip off white spaces first
      field_data = field_data.strip()

    if (field_data != ''):  # Only add non-empty fields to dictionary
      return_rec[field_name] = field_data

  # Set hidden fields
  #
  return_rec['_rec_num_'] = rec_num
  return_rec['_dataset_name_'] = self.name
  return return_rec      

© Stack Overflow or respective owner

Related posts about python

Related posts about performance-tuning