Efficient multi-slicing technique?
google at mrabarnett.plus.com
Sun Jan 25 20:12:29 CET 2009
python at bdurham.com wrote:
> Is there an efficient way to multi-slice a fixed with string into
> individual fields that's logically equivalent to the way one would slice
> a delimited string using .split()?
> Background: I'm parsing some very large, fixed line-width text files
> that have weekly columns of data (52 data columns plus related data). My
> current strategy is to loop through a list of slice()'s to build a list
> of the specific field values for each line. This is fine for small
> files, but seems inefficient. I'm hoping that there's a built-in (C
> based) or 3rd party module that is specifically designed for doing
> multiple field extractions at once.
You could try the struct module:
>>> import struct
>>> struct.unpack("3s4s1s", b"123abcdX")
('123', 'abcd', 'X')
More information about the Python-list