RE: Fixed-length text file to database script

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Edwin.Madari@VerizonWireless.com

    RE: Fixed-length text file to database script

    #your thought is right.
    =============== =============== =============== ==========
    def sizes2fields(si zes):
    d = []
    begin = 0
    for i in sizes:
    if begin:
    end = begin + i
    else: end = i
    d.append((begin , end))
    begin += i
    return tuple(d)

    def slicestring(s, fields):
    d = []
    for i in fields:
    d.append(s[i[0]:i[1]])
    return tuple(d)

    sizes = [16,4,8,8,8]
    s = '12345678901234 567890123456789 012345678901234 567890123456789 0'
    print slicestring(s, sizes2fields(si zes))
    =============== =============== =============== =============
    prints out:
    ('1234567890123 456', '7890', '12345678', '90123456', '78901234')

    hope it helps.
    thanks Edwin

    -----Original Message-----
    From: python-list-bounces+edwin.m adari=verizonwi reless.com@pyth on.org
    [mailto:python-list-bounces+edwin.m adari=verizonwi reless.com@pyth on.org]
    On Behalf Of Eric Wertman
    Sent: Thursday, August 14, 2008 1:59 PM
    To: python-list@python.org
    Subject: Re: Fixed-length text file to database script


    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 :
    --



    The information contained in this message and any attachment may be
    proprietary, confidential, and privileged or subject to the work
    product doctrine and thus protected from disclosure. If the reader
    of this message is not the intended recipient, or an employee or
    agent responsible for delivering this message to the intended
    recipient, you are hereby notified that any dissemination,
    distribution or copying of this communication is strictly prohibited.
    If you have received this communication in error, please notify me
    immediately by replying to this message and deleting it and all
    copies and backups thereof. Thank you.


  • John Machin

    #2
    Re: Fixed-length text file to database script

    On Aug 15, 4:55 am, Edwin.Mad...@Ve rizonWireless.c om wrote:
    #your thought is right.
    =============== =============== =============== ==========
    def sizes2fields(si zes):
    d = []
    begin = 0
    for i in sizes:
    if begin:
    end = begin + i
    else: end = i
    d.append((begin , end))
    begin += i
    return tuple(d)
    Those who are not paid by the keystroke and/or prefer to expend
    keystrokes on meaningful names might like an alternative like this:
    def sizes2offsets(s izes):
    offsets = []
    begin = 0
    for size in sizes:
    end = begin + size
    offsets.append( (begin, end))
    begin = end
    return offsets

    Comment

    • Lie

      #3
      Re: Fixed-length text file to database script

      On Aug 15, 7:12 am, John Machin <sjmac...@lexic on.netwrote:
      On Aug 15, 4:55 am, Edwin.Mad...@Ve rizonWireless.c om wrote:
      >
      #your thought is right.
      =============== =============== =============== ==========
      def sizes2fields(si zes):
         d = []
         begin = 0
         for i in sizes:
            if begin:
               end = begin + i
            else: end = i
            d.append((begin , end))
            begin += i
         return tuple(d)
      >
      Those who are not paid by the keystroke and/or prefer to expend
      keystrokes on meaningful names might like an alternative like this:
      def sizes2offsets(s izes):
        offsets = []
        begin = 0
        for size in sizes:
          end = begin + size
          offsets.append( (begin, end))
          begin = end
        return offsets
      This is even shorter: (and IMHO, clearer)

      def split(s, fields):
      ret = []
      for field in fields:
      s, d = s[field:], s[:field]
      ret.append(d)
      return ret

      sizes = [16, 4, 8, 8, 8]
      s = '12345678901234 567890123456789 012345678901234 567890123456789 0'
      print split(s, sizes)

      alternatively, if what you've got is the partition position instead of
      field width:

      def split(s, sizes):
      ret = []
      start = sizes[0]
      for end in sizes[1:]:
      r, start = s[start: end], end
      ret.append(r)
      return ret

      sizes = [0, 16, 20, 28, 36, 44]
      s = '12345678901234 567890123456789 012345678901234 567890123456789 0'
      print split(s, sizes)

      Michael Stroder:
      If the input data has to be pre-processed before storing it into the
      database a Python script would be needed.
      But not for converting the fixed length string, you could just have
      SQL process the fixed length string then retrieve it back as separate
      fields.

      Comment

      Working...