On Aug 27, 1:35 pm, Mark Elston <[EMAIL PROTECTED]> wrote: > * RyanL wrote (on 8/27/2007 10:59 AM): > > > > > > > I'm a newbie! I have a non-delimited data file that I'd like to > > convert to delimited. > > > Example... > > Line in non-delimited file: > > 0139725635999992000010100534+42050-102800FM-15+1198KAIA > > > Should be: > > 0139,725635,99999,2000,01,01,00,53,4,+42050,-102800,FM-15,+1198,KAIA > > > What is the best way to go about this? I've looked all over for > > examples, help, suggestions, but have not found much. CSV module > > doesn't seem to do exactly what I want. Maybe I'm just missing > > something or not using the correct terminology in my searches. Any > > assistance is greatly appreaciated! Using Python 2.4 > > Since you have to know, a priori, how to break the input string I > assume that these fields are of fixed length. You can use the following > to do what you want: > > >>> a="0139725635999992000010100534+42050-102800FM-15+1198KAIA" > >>> print "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s" % > (a[0:4],a[4:10],a[10:15],a[15:19],a[19:21],a[21:23],a[23:25], > a[25:27],a[27],a[28:34],a[34:41],a[41:46],a[46:51],a[51:]) > > which results in the following output: > > 0139,725635,99999,2000,01,01,00,53,4,+42050,-102800,FM-15,+1198,KAIA > > Mark
Or try this: import struct test = '0139725635999992000010100534+42050-102800FM-15+1198KAIA' template = '4s6s5s4s2s2s2s2s1s6s7s5s5s4s' the_line = struct.unpack(template,test) print the_line print ','.join(the_line) ## ('0139', '725635', '99999', '2000', '01', '01', '00', '53', '4', '+42050', '-102800', 'FM-15', '+1198', 'KAIA') ## ## 0139,725635,99999,2000,01,01,00,53,4,+42050,-102800,FM-15,+1198,KAIA -- http://mail.python.org/mailman/listinfo/python-list