Python2

From vwiki
Revision as of 11:02, 26 May 2011 by Sstrutt (talk | contribs) (Added "XML")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Getting Started

The main site for downloads, documentation etc, can be found at http://www.python.org.

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 http://wiki.python.org/moin/Python2orPython3 for further info.

This page is for Python2

Install

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).

Variables

Variable Information

Get a variables available methods...

dir(variable)

Variable Conversion

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

Files

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

# 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')
log.close

# 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')

XML

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

MiniDOM

Example of processing of an XML file...

from xml.dom.minidom import parseString

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

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

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


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


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

Example XML file (edited excerpt from a TfL feed, see the following of your interested - http://www.tfl.gov.uk/businessandpartners/syndication/)

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