rschroev_nospam_ml at fastmail.fm
Wed Mar 2 16:48:03 CET 2005
Daniel Skinner wrote:
> If I have the following text
> var = '1,2,3,4'
> and I want to use the comma as a field delimeter and rearrange the
> fields to read
> How would I accomplish this in python?
I take it you want to swap the second and the third element? That can be
accomplished by splitting into a list of fields, swap the second and
third element, join the fields back to a string:
>>> var = '1,2,3,4'
>>> fields = var.split(',')
['1', '2', '3', '4']
>>> fields, fields = fields, fields
['1', '3', '2', '4']
>>> newvar = ','.join(fields)
"Codito ergo sum"
More information about the Python-list