[Tutor] unpack/regexp

doug shawhan doug.shawhan at gmail.com
Tue Apr 11 17:32:41 CEST 2006


I always slice the string  in this sort of situation:

s = "12345678901234567890123456789012 "

t = s[:10],s[10:20],s[20:-1]

print t

('1234567890', '1234567890', '123456789012')

One could always bracket it to make a list or whatever.

Hope this helps!



On 4/11/06, Paul Kraus <pkraus at pelsupply.com> wrote:
>
> Ok sorry for the perl refernce but I can't figure out how to do this.
> I have a fixed width text file i need to parse.
>
> so lets say I want an array to containt the pieces i need.
> if the fields I want are lengths from left to right.
> 10 10 13
> 12345678901234567890123456789012
> I want to turn this into an array that has these elements.
> 1234567890
> 1234567890
> 123456789012 <--notice white space
>
> In Perl its a simple
> my @array = unpack ( "A10 A10 A13" , $line )
> this extracts it and removes the whitespace after doing so.
>
> or if i wanted i could do
> my @array = ( $1, $2, $3 ) if ( $line =~ m/^(.{10})(.{10})(.{13}) )
>
>
> --
> Paul Kraus
> =-=-=-=-=-=-=-=-=-=-=
> PEL Supply Company
> Network Administrator
> 216.267.5775 Voice
> 216.267.6176 Fax
> www.pelsupply.com
> =-=-=-=-=-=-=-=-=-=-=
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060411/6fd722a7/attachment.html 


More information about the Tutor mailing list