[Tutor] Help with Re to extract fixed width data fields

Ben Beuchler insyte@emt-p.org
Thu, 14 Sep 2000 18:25:52 -0500


On Fri, Sep 15, 2000 at 09:40:17AM +1200, Phil Bertram wrote:

> I have data that is of fixed length but with no deliminaters
> 
> eg. 3456Customer9878Product    etc etc etc
> 
> I have been using a regular expression (in the form as shown below) to extract the fields. The real data has over 30 fields so the expression is very large.
> Is there a better way ?
> 
> fields=re.match('(.{4,4})(.{8,8})(.{4,4})(.{7,7})',line,0) 

You say it is always fixed length?  If so, you can use indices
(indexes?) to extract it, like so:

>>> mystring = "3456Customer9878Product"
>>> mystring[4:12]
'Customer'

Very spiffy, fast, and requires no extra modules...

Ben

-- 
Ben Beuchler                                         insyte@bitstream.net
MAILER-DAEMON                                         (612) 321-9290 x101
Bitstream Underground                                   www.bitstream.net