25
Python Classes and Objects Release 4.7.0 G A Vignaux February 05, 2015

Python Classes and Objects - sms.wgtn.ac.nz€¦ · Classes and their objects partake of the dynamic nature of Python: they are created at runtime, and can be modified further after

  • Upload
    others

  • View
    14

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Python Classes and Objects - sms.wgtn.ac.nz€¦ · Classes and their objects partake of the dynamic nature of Python: they are created at runtime, and can be modified further after

Python Classes and ObjectsRelease 4.7.0

G A Vignaux

February 05, 2015

Page 2: Python Classes and Objects - sms.wgtn.ac.nz€¦ · Classes and their objects partake of the dynamic nature of Python: they are created at runtime, and can be modified further after
Page 3: Python Classes and Objects - sms.wgtn.ac.nz€¦ · Classes and their objects partake of the dynamic nature of Python: they are created at runtime, and can be modified further after

CONTENTS

1 Introduction 1

2 Classes and Objects 32.1 Defining a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32.2 The Simplest Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42.3 Special Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52.4 A Ship Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62.5 Importing a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72.6 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

3 Python Class Variables 113.1 Class Variables and Object Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113.2 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

4 Using an imported class: Reading csv Files 134.1 Reading a csv file using string functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134.2 Using the csv module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144.3 Reading a csv File into a Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144.4 Writing a csv File using the csv module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

5 Playing with Data 175.1 Reading a csv file using string functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175.2 Using eval . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185.3 Problems with commas inside quotes: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185.4 Extracting the data as a list of lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

6 Indices and tables 19

Index 21

i

Page 4: Python Classes and Objects - sms.wgtn.ac.nz€¦ · Classes and their objects partake of the dynamic nature of Python: they are created at runtime, and can be modified further after

ii

Page 5: Python Classes and Objects - sms.wgtn.ac.nz€¦ · Classes and their objects partake of the dynamic nature of Python: they are created at runtime, and can be modified further after

CHAPTER

ONE

INTRODUCTION

Objects are Python constructs that can contain arbitrary amounts and kinds of data. They also have executable ele-ments, like functions, called methods. Objects of a particular class are described and constructed by a class object.Classes and their objects partake of the dynamic nature of Python: they are created at runtime, and can be modifiedfurther after creation.

Compared with other programming languages, Python adds classes with a minimum of new syntax and semantics butJava programmers should be aware that there are differences between classes in Python and Java.

Here I show how to define a Python class, use it to create objects of that class, and to use the objects. I also illustrateimporting and using a class from the Python csv module.

Note: classes are not the same as modules.

See also:

The section on Classes in the Python Tutorial (http://docs.python.org/tutorial/classes.html) for another view.

1

Page 6: Python Classes and Objects - sms.wgtn.ac.nz€¦ · Classes and their objects partake of the dynamic nature of Python: they are created at runtime, and can be modified further after

Python Classes and Objects, Release 4.7.0

2 Chapter 1. Introduction

Page 7: Python Classes and Objects - sms.wgtn.ac.nz€¦ · Classes and their objects partake of the dynamic nature of Python: they are created at runtime, and can be modified further after

CHAPTER

TWO

CLASSES AND OBJECTS

Although Python can be used in any fashion, and many people use the language straightforwardly, the language isfundamentally object-oriented. This means that the power and flexibility of classes and objects is available whenappropriate. You will be frequently importing classes from other people’s modules of interest so you will need toknow how to use them in any case.

A class is a group of objects all of the same type. It is represented by a Python class object that acts as a blueprint,or plan, to describe objects or instances of the class. Each instance object can have its own data (called attributes)and its own functions (called methods). Defining a class does not, in itself, create any instances. That has to be doneseparately. Classes are described in detail in most Python books.

Perhaps, without being aware of it, you have been using objects from the start of your Python experience, though theyare often hidden from you. For example, lists are objects of a list class and have methods associated with them. IfL is a list (an object of the list class) it has a reverse method which acts on the list:

>>> L = [1,2,3]>>> L.reverse()>>> L[3, 2, 1]

You need to know about classes because many Python modules provide their services in the form of special classeswhich you import and then create instances for your own use.

You may need to define your own classes and objects. For example, you could have a class of Earthquake to createmany instances of Earthquake observations, each holding data on one incident. Or you could define a class calledShip (see A Ship Class) then each instance holds data and behavior for an individual ship.

2.1 Defining a Class

Before you can create objects you must define their class. This is done using the class statement which has thefollowing general structure:

class ClassName(object):<body>

where <body> is a list of Python statements, setting variable values and defining functions (methods). Here, the classClassName inherits from the super-class object

To create an object of the class we use the Class name as a function to return an object:

instance1 = ClassName()

Now instance1 is an instance, or object, of the class ClassName.

3

Page 8: Python Classes and Objects - sms.wgtn.ac.nz€¦ · Classes and their objects partake of the dynamic nature of Python: they are created at runtime, and can be modified further after

Python Classes and Objects, Release 4.7.0

2.2 The Simplest Class

As an illustration, let us define a very simple class of Thing. For the moment, the objects of this class hold no dataand have no methods:

>>> class Thing(object):... """ This is a docstring """... pass

The Class name always starts with a capital letter (Thing) and it is usual to name the instances (objects) of the classstarting with a lower case letter (though this is not compulsory). I have given it a docstring which describes the classand can also be extracted by Python code. As the definition must have some sort of statement, here I just used thePython statement that does nothing: pass.

2.2.1 Creating an object

To create an instance ( i.e. an object) of class Thing we use the class name as a function to return the object. Herewe create two Thing objects:

>>> thing1 = Thing()>>> thing2 = Thing()

We can put them into a box:

>>> box = [thing1,thing2]

2.2.2 Things with data

An object holds its own data in its fields (its attributes or variables). The fields are referred to by a dot notation(object.attribute). They are known as object variables (to contrast them with class variables, which are different andare discussed in Chapter Python Class Variables).

We can provide existing objects with attributes at any time in the program. It is usually more convenient to give themattributes when they are created (for this, see the section on the __init__ method). Here we give each Thing objectattributes of name, and colour after creation:

>>> thing1.name = 'Thing1'>>> thing1.colour = 'red'>>> thing2.name = 'Thing2'>>> thing2.colour = 'blue'>>> print thing1.colour, thing2.colourred blue>>> for each in box:... print each.name...Thing1Thing2

2.2.3 Things with actions - methods

Objects of a class can also have behaviour attributes. Called methods, these look very much like functions. They aredefined in the Class definition.

Now the Thing class is extended by a method called setColour so as to set the colour of the Thing object:

4 Chapter 2. Classes and Objects

Page 9: Python Classes and Objects - sms.wgtn.ac.nz€¦ · Classes and their objects partake of the dynamic nature of Python: they are created at runtime, and can be modified further after

Python Classes and Objects, Release 4.7.0

>>> class Thing(object):... """ A class with a method """... def setColour(self, col):... """ Sets the colour attribute to col """... self.colour = col...>>> thing1 = Thing()>>> thing2 = Thing()

The definition of the setColour method shows that it is defined just like a function but the first argument is alwaysself which refers to the particular object that is using the method.

Inside the method, the attributes are referred to using the self prefix (e.g. self.colour). Compare this to the useof the object identifier when you set the colour from outside the object. In both cases the structure is object.attribute.

There may be many Things in existence and we may wish to call the setColour method on each one separately.To use the method for a particular object we execute it using the dot notation but without the self argument:

>>> thing1.setColour('red')>>> thing2.setColour('blue')>>> print thing1.colour,thing2.colourred blue

It is recognised good Python practice to give every class and every method a docstring (Documentation string) as wehave here. It is the triple-quoted string immediately following the def line.

2.3 Special Methods

Python allows you to define some special methods for all classes. You can recognize them as they have names startingand ending with double underlines. For example, __init__, __str__, __repr__. These have a special meaningfor Python. They are defined as methods but are not explicitly used by the programmer. They are called automaticallyby Python where they are needed. There are many special methods but here we introduce two which are immediatelyuseful to us, __init__ and __str__.

2.3.1 __init__

The __init__ method is automatically called when a new object is created if one has been defined in a class. Thisgives us an opportunity to initialise any object attribute values. So that instead of setting attributes from outside orusing a special method like setcolour after an object is created, you can initialise the attributes when it is created.

When the method is defined the first argument must be self but other arguments can also be passed to the object. Forthe Thing example, the __init__ method, below, will initialise the colour attribute of the Thing object. Sincewe are assigning it inside the object we must prefix the attribute with self. Since we no longer need the setColourmethod I have not included it here:

>>> class Thing(object):... """ A class with an __init__ """... def __init__(self,col):... """ initialises the colour attribute """... self.colour = col...>>> thing1=Thing('Red')>>> thing2=Thing('Blue')>>> print thing1.colourRed

2.3. Special Methods 5

Page 10: Python Classes and Objects - sms.wgtn.ac.nz€¦ · Classes and their objects partake of the dynamic nature of Python: they are created at runtime, and can be modified further after

Python Classes and Objects, Release 4.7.0

Let us improve __init__ to define a name attribute as well:

>>> class Thing(object):... """ A thing class """... def __init__(self,nm,col):... """ Initialises the name and colour attribute """... self.name = nm... self.colour = col...>>> thing1=Thing('THING1','Red')>>> thing2=Thing('THING2','Blue')

Once an object has been given a name and a colour it carries them around during the program:

>>> print thing1.name, thing1.colourTHING1 Red

2.3.2 __str__

It is sometimes useful to define a special method called __str__. This should construct and return a string thatdisplays data for the object in a clear format. If it has been defined, Python uses it whenever you print the object.You then get the data automatically printed out in a way convenient to you.

For example, let us add a __str__ method to the Thing definition. Recall that both class.name and self.colour havestring values so that self.name+’ is ’+self.colour is returned as a string. When you print a Thingobject Python will construct and print the string that __str__ returns:

>>> class Thing(object):... """ A class with a __str__ method """... def __init__(self,nm,col):... """ Initialises the name and colour attribute """... self.name = nm... self.colour = col... def __str__(self):... """ A nicely formatted string"""... return self.name+' is '+self.colour

>>> thing1=Thing('THING1','Red')>>> print thing1 # this will call __str__() for the objectTHING1 is Red

Even though we never explicity called the __str__ method in our program it was recognised and used by Pythonwhen needed (and __init__ acts in the same way).

2.4 A Ship Class

This is a richer example: a Ship class. Each ship will have a namewhich must, of course, be an attribute. On creationof a ship we will also set the load and location to initial values. We also define methods to set or change theload or location. Here is the definition:

class Ship(object):"""A class of ships"""def __init__(self,nm):

""" Initialise a Ship """self.name = nm

6 Chapter 2. Classes and Objects

Page 11: Python Classes and Objects - sms.wgtn.ac.nz€¦ · Classes and their objects partake of the dynamic nature of Python: they are created at runtime, and can be modified further after

Python Classes and Objects, Release 4.7.0

self.load = 0self.location = (0.0,0.0)

def setload(self,ld):""" Set the Ship's load """self.load = ld

def setlocation(self,loc=(0.0, 0.0)):""" Set the Ship's location, a tuple """self.location = loc

def __str__(self):""" A descriptive string of the shipgiving its location. """return self.name+' is at '+str(self.location)

In method __str__ we return a string which includes the Ship name and location. The str call in the returnstatement converts the location tuple into a string. It is not the same as the __str__ method.

We can create and use a Ship object. We assume that the class has been defined in shipmodule.py:

>>> from shipmodule import Ship>>> arahura = Ship('Arahura')>>> arahura.setload(1000)>>> arahura.setlocation((-50,175))>>> print arahuraArahura is at (-50, 175)

2.5 Importing a Class

You can import a class from a module. This is a convenient way of structuring a project. For example, I made a fileshipmodule.py to hold the Ship definition and can import the class from there. Once imported, I could instantiate(i.e. create) Ship objects in the usual way, just as if I had included the definition in the same file :

>>> from shipmodule import Ship>>> hermes = Ship('Hermes')>>> pinafore = Ship('Pinafore')>>> hermes.setload(1000.0)>>> hermes.setlocation((-50,175))>>> print hermes.location(-50, 175)>>> print hermesHermes is at (-50, 175)

2.6 Inheritance

You can construct one class as a sub-class of another or a number of others. The parent classes are called base-classes.

Assume there is only one base class. The sub-class inherits from it in the sense that it can inherit any methods andattributes defined in the base-class. The sub-class may modify the methods and attributes defined by the base-classand also define its own new methods and attributes.

For example, suppose that we wish to define a class Hydrofoil using Ship as a base-class. We will use thesetload and setlocation methods but we need to change the definition to add a passengers attribute to hold

2.5. Importing a Class 7

Page 12: Python Classes and Objects - sms.wgtn.ac.nz€¦ · Classes and their objects partake of the dynamic nature of Python: they are created at runtime, and can be modified further after

Python Classes and Objects, Release 4.7.0

the number of passengers and a speed attribute to hold the current speed. We will write setpassengers andsetspeed methods and correspondingly change __init__ to set the number of passengers and speed to zero. Wemust call Ship.__init__(self,nm) to initialise the base class.

We will also modify the __str__ method. We do not plan to use the setload method as the Hydrofoils only carrypassengers but it does not have to be removed.

Inheritance is specified in the program by putting the name of the parent class or classes in brackets in the classstatement. You can see how Hydrofoil inherits from Ship in the first line of the class definition:

from shipmodule import Ship

class Hydrofoil(Ship):"""A class of Hydrofoils"""def __init__(self,nm):

""" Initialise a HydrofoilShip has location and load """

Ship.__init__(self,nm) # initialise Shipself.passengers = 0self.speed = 0.0

def __str__(self):""" A descriptive string of the HydrofoilThis constructs a formatted string"""output = """name=%10s, speed=%5.2f, passengers=%3d"""return output%(self.name,self.speed,self.passengers)

def setpassengers(self,pax):""" Set the number of passengers """self.passengers = pax

def setspeed(self,v):""" Set the speed """self.speed = v

For convenience, I saved this in a file hydrofoil.py and imported the class:

>>> from hydrofoil import Hydrofoil>>> halibut = Hydrofoil('Halibut')>>> halibut.setspeed(20)>>> halibut.setpassengers(20)>>> halibut.setlocation((-50,175))>>> print halibutname= Halibut, speed=20.00, passengers= 20

It is important to realize that setlocation method did not need to be defined because it is automatically availableto Hydrofoils as it is a method of the parent Ship class,.

Note that when an object of the sub-class is created, and therefore calls the __init__ method, this does not auto-matically call the parent class’s __init__. If you need to call it to set attributes then you can call it as part of thesub-class __init__. This is done using the base-class name in the form Ship.__init__.

Similarly, we could introduce a Tug, a new sub-class of Ship which would perhaps not have load or passengersattributes but might have a capacity value.

8 Chapter 2. Classes and Objects

Page 13: Python Classes and Objects - sms.wgtn.ac.nz€¦ · Classes and their objects partake of the dynamic nature of Python: they are created at runtime, and can be modified further after

Python Classes and Objects, Release 4.7.0

2.7 Summary

A class can create a number of objects all of the same type. It is represented by Python class object that acts as ablueprint, or plan, to describe objects or instances of the class. A class is defined using the class statement and anew instance object is returned by using the class name as a function.

A method of a class is defined using def and all methods have self as their first argument in the definition. This isrequired as the first argument when the method is defined. But the self is not used when the methods are called.

The __init__ method will be executed when the classname is used to construct a new instance. It lets you initialisethe fields of the new object.

Within the object, these data fields (or attributes) are referred to using the self as a prefix. So the ship’s load willbe referred to as self.load in any of the methods of the ship. Outside the object the fields are referred to with theobject as a prefix. For example, outside the ship hermes its load is hermes.load.

2.7. Summary 9

Page 14: Python Classes and Objects - sms.wgtn.ac.nz€¦ · Classes and their objects partake of the dynamic nature of Python: they are created at runtime, and can be modified further after

Python Classes and Objects, Release 4.7.0

10 Chapter 2. Classes and Objects

Page 15: Python Classes and Objects - sms.wgtn.ac.nz€¦ · Classes and their objects partake of the dynamic nature of Python: they are created at runtime, and can be modified further after

CHAPTER

THREE

PYTHON CLASS VARIABLES

A class can be given class variables. These are different from attributes discussed so far. They are defined andinitialized when the class is defined. Class variables are unique to the class and all class objects share the samevalue – in contrast to object variables which are local to each object of the class.

They are used to record data that is appropriate for the class as a whole, not for any particular object. For example,you might use a class variable to record the number of messages rejected from a communication system. Only onecount is sensible, the total number. When a message is rejected it can add to the count.

3.1 Class Variables and Object Variables

Class variables are created and initialized when the class is defined. They are referred to by the class name (e.g.Ship.count) both inside and outside the class.

Contrast object variables with class variables: Each individual object (known as an instance of the class) has its owndata. Thus pinafore.load is not the same as hermes.load. The object variables can be set when an object iscreated (using __init__) or afterwards and are referred to inside the object with the prefix self and outside withthe object prefix (e.g. hermes.load).

The class variables are referred to using the class name as a prefix (e.g. Ship.total_number) for the Ship class,where total_number records the total number of ships (see below).

3.1.1 Example: Counting the number of ships

Suppose we need to record the total number of ships that have been launched. There can be only one total_number,not a different one for each object created. This is a case for a class variable.

We will modify the Ship class to use a class variable, total_number in file shipmodule2.py. This will holdthe current count of the number of ships and is set to 0 when the class is defined. It is defined within the class definitionbut outside the methods. For the Ship case, this would be referred to as Ship.total_number.

When a new ship is launched, its __init__ is executed and as well as setting its object variables we incrementtotal_number using Ship.total_number += 1:

class Ship(object):""" shipmodule2: A class of ships"""total_number=0 # Class Variable

def __init__(self,nm):""" Initialise a Ship """

Ship.total_number += 1 ## incremented HERE ##

11

Page 16: Python Classes and Objects - sms.wgtn.ac.nz€¦ · Classes and their objects partake of the dynamic nature of Python: they are created at runtime, and can be modified further after

Python Classes and Objects, Release 4.7.0

self.name = nm ## object variablesself.load = 0self.location = (0.0,0.0)

def setload(self,ld):""" Set the Ship's load """self.load = ld

def setlocation(self,loc=(0.0, 0.0)):""" Set the Ship's location as a tuple """self.location = loc

def __str__(self):""" A descriptive string for the ship,

giving its location."""return self.name+' is at '+str(self.location)

Now use the class:

>>> from shipmodule2 import Ship>>> print 'Ship.total_number=',Ship.total_numberShip.total_number= 0>>> hermes = Ship("Hermes")>>> print 'Ship.total_number =',Ship.total_number, hermes.nameShip.total_number = 1 Hermes>>> lynx = Ship("Lynx")>>> print 'Ship.total_number =',Ship.total_number, lynx.nameShip.total_number = 2 Lynx

3.2 Summary

Class variables are created when the class is defined. There is only one for the class as a whole, not one for eachobject.

They are defined in your listing at the same level as the methods, not inside them. They can be changed by objects ofthe class or from elsewhere. They are referred to in the form classname.variablename (and not self.variablename).

12 Chapter 3. Python Class Variables

Page 17: Python Classes and Objects - sms.wgtn.ac.nz€¦ · Classes and their objects partake of the dynamic nature of Python: they are created at runtime, and can be modified further after

CHAPTER

FOUR

USING AN IMPORTED CLASS: READING CSV FILES

Spreadsheet programs can save their data in a pure-text Comma Separated Variable (csv) format so that other appli-cations can make use of the data. Each row in the spreadsheet is written to the file with the contents of the columnsseparated by commas. The format may differ between different spreadsheet programs but Excel is generally consideredto be the standard.

This chapter examines how we could read and write csv data using two techniques:

• A simple approach using the string split(’,’) method to divide each line

• An approach using the csv module which defines special objects and methods.

Assume we have a file, data/observations.csv, exported from Excel, and we wish to read it for later process-ing. The data I obtained has the form:

Time,Voltage10.234,19.011.346,20.011.768,19.412.432,21.513.555,20.1

4.1 Reading a csv file using string functions

This is the method that immediately springs to mind. It uses the string split(’,’) method. We read the file as asequence of strings, one for each line of the file. The strings will be terminated by a newline character. We processeach line, splitting it at the commas into a series of items, each of which is a string. The items may need to be convertedto a number before doing any arithmetic using the built-in float or int functions. The following example scriptreads the data/observations0.csv file, and sums the second item in each line:

13

Page 18: Python Classes and Objects - sms.wgtn.ac.nz€¦ · Classes and their objects partake of the dynamic nature of Python: they are created at runtime, and can be modified further after

Python Classes and Objects, Release 4.7.0

>>> inputlines = open('data/observations0.csv','r')>>> sum = 0.0>>> for line in inputlines:... split_line = line.split(',')... if split_line[0] != 'Time':... sum += float(split_line[1])...>>> inputlines.close()>>> print 'sum= ',sumsum= 100.0

This simple approach will usually work well unless any of the data in the original spreadsheet contains commas as caneasily happen. The csv module, dealt with next, handles this case.

4.2 Using the csv module

The csv module implements classes to read and write tabular data in csv format. There are two classes defined:reader and writer. They construct objects which read and write sequences. reader returns a list of stringsfor each line of the file. So it combines the normal read with an automatic split into strings. Here I have usedinput_object as the name of the reader object:

>>> import csv>>> inputfile = open('data/observations0.csv','r')>>> input_object = csv.reader(inputfile) # a reader object>>> sum = 0.0>>> for line in input_object:... if line[0] != 'Time':... sum += float(line[1])...>>> print 'sum= ',sumsum= 100.0

As you can see numeric values will be in string form and will have to be converted. Closing a file is optional for sucha small script. file.

4.3 Reading a csv File into a Dictionary

Many Pythoneers find it easier to work with a dictionary than a list. The code is then both more readable and, onceyou are familiar with using dictionaries, easier to modify if the form of the original data changes. The csv modulecan automatically return a dictionary instead of a list for each line if you use the DictReader class. There is acorresponding DictWriter class to write a csv formatted file from a list of dictionaries.

in this example we read the same file as before but this time as a list of dict. Each item in the list is a separatedictionary corresponding to a line of the csv file.

>>> import csv>>> inputfile = open('data/observations0.csv','r')>>> input_object = csv.DictReader(inputfile)>>> for line in input_object:... if line['Voltage']: # i.e. if this exists... print 'V = ',line['Voltage'],' t = ',line[ 'Time']V = 19.0 t = 10.234V = 20.0 t = 11.346V = 19.4 t = 11.768

14 Chapter 4. Using an imported class: Reading csv Files

Page 19: Python Classes and Objects - sms.wgtn.ac.nz€¦ · Classes and their objects partake of the dynamic nature of Python: they are created at runtime, and can be modified further after

Python Classes and Objects, Release 4.7.0

V = 21.5 t = 12.432V = 20.1 t = 13.555

The keys used are the headers of the columns in the csv file

4.4 Writing a csv File using the csv module

Just as it can read csv files, the module can also write them so data can be exported to a spreadsheet. You prepare alist of rows, each of which is a list with the key and the value. Each list contains the data you need to write onto onerow of the spreadsheet. Here I construct a list of three rows in the listofrows list and write it out. That would haveto be imported into the spreadsheet and would form three rows.

>>> import csv>>> list_of_rows = [ ['Joe',100],['Tony',130], ['Fred',152] ]>>> outputfile = open('output.csv','w')>>> writer = csv.writer(outputfile)>>> writer.writerows(list_of_rows)

4.5 Summary

Using Python as a “glue” language by reading and writing csv files can be very useful in tying systems together.

The csv module is not essential because a csv file is just text and it can be read and written using normal Pythonread and write commands.

But the module is convenient and carries out the required splitting and conversions automatically, even the complicatedcase where fields contain commas.

The module can also read into or write from data held in a dictionary.

See also:

The csv module: (http://docs.python.org/library/csv.html#module-csv) Documentation on the csv module fromthe Python Standard Library

4.4. Writing a csv File using the csv module 15

Page 20: Python Classes and Objects - sms.wgtn.ac.nz€¦ · Classes and their objects partake of the dynamic nature of Python: they are created at runtime, and can be modified further after

Python Classes and Objects, Release 4.7.0

16 Chapter 4. Using an imported class: Reading csv Files

Page 21: Python Classes and Objects - sms.wgtn.ac.nz€¦ · Classes and their objects partake of the dynamic nature of Python: they are created at runtime, and can be modified further after

CHAPTER

FIVE

PLAYING WITH DATA

This chapter contains several illustrations where Python is used to handle data. Often one’s data comes in the form ofa csv file exported from a spreadsheet and this form will be used in the examples. This is what it looks like:

Time,Voltage,Observer10.234,19.0,OK11.346,20.0,"GAV drunk"11.768,19.4,"GAV bored"12.432,21.5,SC13.555,20.1,"MJ clear-minded"

The data has a header for each of its three columns and a series of lines with numbers and strings separated by com-mas. The Observer column contains strings with double-quotes. These are produced automatically by spreadsheetprograms, particularly if any contents contain commas.

Note: In Europe, where commas are used instead of a point in numbers, the csv values are separated by semicolons.

Our objective is to read this data and carry out arithmetic operations on it. Just as an example to test out methods,this might be to sum the values in a column (which could easily be done on the original Excel file) or to add constantvalues to every entry.

5.1 Reading a csv file using string functions

We will start with a simple version of this example, containing just the first two columns.

Time,Voltage10.234,19.011.346,20.011.768,19.412.432,21.513.555,20.1

This is the method that immediately springs to mind. We read the file as a sequence of strings, one for each line of thefile. The strings will be terminated by a newline character. We process each line, using string split(’,’) to divideit at the commas into a series of items, each of which is a string. Then the items are converted to a number beforedoing any arithmetic using the built-in float or int functions.

The following example script reads the data/observations0.csv file, and sums the second item in each line:

>>> inputlines = open('data/observations0.csv','r')>>> sum = 0.0>>> for line in inputlines:... split_line = line.split(',')[:2]

17

Page 22: Python Classes and Objects - sms.wgtn.ac.nz€¦ · Classes and their objects partake of the dynamic nature of Python: they are created at runtime, and can be modified further after

Python Classes and Objects, Release 4.7.0

... if 'Time' not in split_line:

... sum += float(split_line[1])

...>>> inputlines.close()>>> print 'sum= ',sumsum= 100.0

This simple approach will usually work well unless any of the data in the original spreadsheet contains commas asoften occurs.

5.2 Using eval

The built-in eval function makes this easier:

>>> inputlines = open('data/observations0.csv','r')>>> sum = 0.0>>> for line in inputlines:... if 'Time' not in line:... eval_line = eval(line)... print eval_line... sum += eval_line[1]...(10.234, 19.0)(11.346, 20.0)(11.768, 19.4)(12.432, 21.5)(13.555, 20.1)>>> print sum100.0

5.3 Problems with commas inside quotes:

When you read in csv data, each line is returned as a string and normally we would want to split the lines. If the linecontains

>>> inputlines = open('data/observations2.csv','r')>>> sum = 0.0>>> for line in inputlines:... split_line = line.split(',')... print split_line...['Time', 'Voltage', 'Observer\n']['10.234', '19.0', '"OK"\n']['11.346', '20.0', '"GAV', ' drunk"\n']['11.768', '19.4', '"GAV"\n']['12.432', '21.5', '"SC"\n']['13.555', '20.1', '"MJ', ' clear-minded"\n']

5.4 Extracting the data as a list of lists

we would use “func:split() to separate all the elements.

18 Chapter 5. Playing with Data

Page 23: Python Classes and Objects - sms.wgtn.ac.nz€¦ · Classes and their objects partake of the dynamic nature of Python: they are created at runtime, and can be modified further after

CHAPTER

SIX

INDICES AND TABLES

• genindex

• modindex

• search

19

Page 24: Python Classes and Objects - sms.wgtn.ac.nz€¦ · Classes and their objects partake of the dynamic nature of Python: they are created at runtime, and can be modified further after

Python Classes and Objects, Release 4.7.0

20 Chapter 6. Indices and tables

Page 25: Python Classes and Objects - sms.wgtn.ac.nz€¦ · Classes and their objects partake of the dynamic nature of Python: they are created at runtime, and can be modified further after

INDEX

Symbols__init__, 5__str__, 6

Aattributes

object, 4special method, 5

Cclass

definition, 3example, 3import, 7inheritance, 7variable, 11

class variableexample, 11

Classes,objects, 1create

object, 4csv

reading, 13, 17csv module

dictionary, 14reading, 14writing, 15

Ddefinition

class, 3dictionary

csv module, 14

Eexample

class, 3class variable, 11Ship, 6

Ffields

object, 4

Iimport

class, 7inheritance

class, 7

Mmethod

object, 4

Oobject

attributes, 4create, 4fields, 4method, 4variable, 11

Rreading

csv, 13, 17csv module, 14

SShip

example, 6special method

attributes, 5

Vvariable

class, 11object, 11

Wwriting

csv module, 15

21