rearrange text

Simon Brunning simon.brunning at gmail.com
Wed Mar 2 16:50:54 CET 2005


On 2 Mar 2005 07:36:48 -0800, Daniel Skinner <skinner.daniel at gmail.com> wrote:
> If I have the following text
> 
> var = '1,2,3,4'
> 
> and I want to use the comma as a field delimeter 

That bit's easy:

C:\WINNT\system32>python
Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> var = '1,2,3,4'
>>> split_var = var.split(',')
>>> split_var
['1', '2', '3', '4']

> and rearrange the
> fields to read
> 
> '1,3,2,4'

I don't really understand this rearrangement, but I'll do it the dumb way:

>>> rearranged_var = split_var[0], split_var[2], split_var[1], split_var[3]
>>> rearranged_var
('1', '3', '2', '4')

Then you can join it all up again like so:

>>> ','.join(rearranged_var)
'1,3,2,4'

-- 
Cheers,
Simon B,
simon at brunningonline.net,
http://www.brunningonline.net/simon/blog/



More information about the Python-list mailing list