Fixed-length text file to database script

Eric Wertman ewertman at gmail.com
Thu Aug 14 19:59:22 CEST 2008


I have a machine (PLC) that is dumping its test results into a fixed-
length text file.


While it has nothing to do with python,  I found that creating a MySQL
table with the proper fixed length char() fields and using 'load data
infile'  was the easiest way to deal with that sort of scenario.   The
python script is the secondary part, that handles the normalization
and proper typing of the first table to the second, permanent storage
area.  But in this case, the more advanced bits are the database and
SQL details, and python is just a very convenient way to build the SQL
statements and execute them.

I'm really not sure what the best way to deal with fixed length data
is in python.  I might define a list with the field lengths and use a
string slicing to get the items.. as a first thought:

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

fd = open(myfile,r)

for line in fd.readlines() :

    idx1 = 0
    for l in sizes :



More information about the Python-list mailing list