Main Content

Easy data to object mapping (csv and Python)

Archive - Originally posted on "The Horse's Mouth" - 2016-03-24 08:00:41 - Graham Ellis

Data files with each line being a record, and a number of 'columns' of information relating to each record on each line are a very common way of information being passed around - in essence a spreadsheet of data. And a sensible way for programs to handle that data is for each row / record to be converted into an object, so that data specific code can be hidden within (encapsulated in) a class, and with each column / field / attribute / property then being accessed in its own specific way.

Sometimes there is significant work involved in converting the incoming data into objects (data to object mapping), but at other times it can be straightforward. Take an example from last week's course, where I mapped a data file of UK bus stops into objects. As the data source was in comma separated value (CSV) format, and the langauge in use was Python, I was able to use the csv module that's a part of the Pyton distribution. Reading the data file becomes as single as:

  fh = open("Stops.csv","r")
  getstop = csv.DictReader(fh)
  for place in getstop:
    current = stop(place)


With the obeject defination and constructor as follows:

  class stop(object):
    def __init__(self,table):
      self.info = table


The DictReader class emthod even takes the first line of the data files as headers, and used the field name supplied as the keys for the dict so that my application can then pick up the unique bus stop code as follows:

  def getId(self):
    return self.info["NaptanCode"]


And my 'object to string' mapping for printing is as easy as

  def __str__(self):
    say = (self.info["NaptanCode"],self.info["Easting"],self.info["Northing"],
      self.info["Street"],self.info["LocalityName"])
    return (", ".join(say))


Compplete application to look up any UK bus stop by its code [here] and the data file is [here]. Careful - the data file is as huge as the demo program is tiny!