There are columns

As we saw with the last program there are too many unaligned values in a line to be useful.

What we need to do is to show only what we want to see. The way to pick arbitrary values is to split the line into a list and then taking only the elements we want.

That is were the cvs module will help us. The cvs module's purpose is to make it easier to deal with cvs formated files. We could do it by hand but the cvs module is ready to do it. Its reader() method will take each line of the file and make a list containing all that line's columns. Then we just pick those columns we want.

We know what each column's name is by looking at the first line of the file. Pay attention to the first line shown by the last program. That line is a header line and contains, you guess it, each columns header. The reader() method will create the list in the same order so the first column will have the 0 index, the second the 1 index etc. as in lines 17 to 21.

   1:import csv # This will help us reading csv formated files
   2:
   3:cars_file = open('2006_FE_Guide_14-Nov-2005_download.csv', 'rb')
   4:
   5:# The reader method will put each line
   6:# of the csv file into a list of columns.
   7:reader = csv.reader(cars_file)
   8:
   9:i = 0    # line counter
  10:print
  11:
  12:reader.next() # skip the header line
  13:for line in reader:
  14:
  15:    i = i + 1
  16:
  17:    print line[1],    # manufacturer
  18:    print line[2],    # model
  19:    print line[3],    # displacement in liters
  20:    print line[5],    # transmission type
  21:    print "average mpg is", line[9]
  22:
  23:    if i == 10:
  24:
  25:        i = 0
  26:        print
  27:        raw_input("Hit Enter to continue")
  28:        print
  29:
  30:print
  31:cars_file.close()
  32:

Hiting Ctrl-C may be not adequate for a non programmer user. There is that error message that gives our program an unprofessional look.

To fix it we can test for something that the user types to stop the program. In this case the easiest is to test if the user types anything as in line 31. If he does we break the loop:

   1:import csv # This will help us reading csv formated files
   2:
   3:cars_file = open('2006_FE_Guide_14-Nov-2005_download.csv', 'rb')
   4:
   5:# The reader method will put each line
   6:# of the csv file into a list of columns.
   7:reader = csv.reader(cars_file)
   8:
   9:# the message we will show to the user
  10:message = "Hit Enter to continue or any other char followed by Enter to stop: "
  11:
  12:i = 0    # line counter
  13:print
  14:
  15:reader.next() # Skip the header line
  16:for line in reader:
  17:
  18:    i = i + 1
  19:
  20:    print line[1],    # manufacturer
  21:    print line[2],    # model
  22:    print line[3],    # displacement in liters
  23:    print line[5],    # transmission type
  24:    print "average mpg is", line[9]
  25:
  26:    if i == 10:
  27:
  28:        i = 0
  29:        print
  30:        char = raw_input(message)
  31:        if char != '':    # if char is not empty
  32:            break
  33:        print
  34:
  35:print
  36:cars_file.close()