Splitting Pythons in Bradford
Archive - Originally posted on "The Horse's Mouth" - 2006-11-29 11:45:33 - Graham EllisPython 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