« Christmas in November | Main | Python collections - mutable and imutable »

November 29, 2006

Splitting Pythons in Bradford

Python supports two split functions methods to explode a string into a list of substrings. There's one in the default class that works on a string, and there's another in the re (Regular Expression) class.

Splitting at a string works very will if you've got a fixed delimiter, typically in a machine generated file of data. You write
inputstring.split(separator)
however, with data that's entered by a user and may well contain multiple spaces as the separator, you would be better advised to write
regex.split(inputstring)
and, yes, the parameters are indeed the other way around.

Here's the result of splitting an address which had multiple spaces in in between the company hame and town, using the string and re classes:
['Techsputer', 'Training,', '', 'Bradford', 'on', 'Avon']
['Techsputer', 'Training,', 'Bradford', 'on', 'Avon']

and you can see the full source code here

Posted by gje at November 29, 2006 11:45 AM

Comments

Post a comment




Remember Me?


Well House Consultants Ltd. Copyright 2008