From vWiki
Jump to navigation Jump to search

Getting Started

The main site for downloads, documentation etc, can be found at

Python2 or Python3

The language is going through a metamorphosis from v2 to v3 and you'll have to choose which to go for. Essentially if you're starting from scratch then go for v3, but if you need to integrate with existing Python code or are a complete scripting/coding novice (and so need to be able to use the wealth of examples etc on the web) go for v2. See for further info.

This page is for Python2


Python often comes pre-installed on Linux systems such as Ubuntu, try firing up the command line environment by typing python. Otherwise you'll need to install from a repository (unix), or download and execute tyhe installer (windows).


Variable Information

Get a variables available methods...


Variable Conversion

int(var)                # Convert to integer
str(var)                # Convert to string


# Read contents of file
file = open('file.txt','r')
data =

# Write a line to a file
log = open('test.log', 'w')
log.write('some text, no carriage return at end')
log.writelines('a line of text')

# Get directory listing and print with filesizes
import os
files = os.listdir('dir')
for fil in files:
    print (fil + '\t' + str(os.path.getsize(os.path.join('dir', fil))))

# Move file
import shutil
shutil.move('file', 'dir/file')


There are two branches of modules that enable handling of XML...

  • SAX - Simple API for XML
  • DOM - Document Object Model, which has a cut-down version known as...
    • MiniDOM


Example of processing of an XML file...

from xml.dom.minidom import parseString

# Read file in
file = open('file.xml','r')
data =

# Parse file into dom object
dom = parseString(data)

# Extract XML root data
name = dom.getElementsByTagName('name')[0]
descrip = dom.getElementsByTagName('description')[0]

# Extract XML header data
header = dom.getElementsByTagName('Header')[0]
identifier = header.getElementsByTagName('Identifier')[0]
owner = header.getElementsByTagName('Owner')[0]

# Extract XML events data
events = dom.getElementsByTagName('roadrunner')[0].getElementsByTagName('rr_event')
for event in events:
	ltisid = event.getElementsByTagName('ltisid')[0]
	title = event.getElementsByTagName('title')[0]

Example XML file (edited excerpt from a TfL feed, see the following of your interested -

<?xml version="1.0" encoding="ISO-8859-1"?>
 <name>Transport for London Live Traffic Disruptions</name>
 <description>Transport for London LTIS (London Traffic Information System) Events</description>
  <Identifier>TfL | Events</Identifier>
  <Owner>Transport for London</Owner>
 <roadrunner updatetime="2011-05-07T2151">
   <title>Piccadilly / A4 (West End)</title>
   <title>Marylebone Road / A501 (Regent&#39;s Park)</title>
   <title>Blackwall Tunnel (Both Bores) (Poplar)</title>