Possible to slice a string with unpacked tuple?

python at bdurham.com python at bdurham.com
Sun Jan 25 02:56:15 CET 2009


Hi, I'm the OP in question.

> the OP should simply write myString[slice(*myTuple)]

My tuples define positions in a fixed width string for parsing values.
So rather than 'unpacking' tuples, I'm using slice() to directly define
actual field positions, eg.

field_1 = slice( 4, 7 )
field_2 = slice( 7, 9 )
etc ...

This allows me to extract the field values as follows:

for line in myOpenFile:
    value_1 = line[ field_1 ]
    value_2 = line[ field_2 ]

IMO, this makes a common task like extracting fields very natural and

I LOVE THIS LANGUAGE!! (to echo another recent thread in this forum)


More information about the Python-list mailing list