Fixed-length text file to database script

Eric Wertman ewertman at gmail.com
Thu Aug 14 14:06:24 EDT 2008


Sorry, didn't get to finish my script.  Have to figure out the deal
with gmail and the tab key someday.

myfile = '/somewhere/somefile.txt'
sizes = [16,4,8,8,8]

fd = open(myfile,r)

data = []
for line in fd.readlines() :
    a = []
    idx1 = 0
    for l in sizes :
        idx2 = idx1 + l
        a.append(line[idx1:idx2])
        idx1 += l
    data.append(a)

fd.close()
print data

This isn't tested, and there are probably more elegant ways to do it,
but for quick and dirty I think it should work.



More information about the Python-list mailing list