quickly read a formated file?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lialie

    #1

    quickly read a formated file?

    Hi,
    Is there a fine way to read a formated file like:
    %HEADER
    title = "Untilted"
    username = "User"

    %DATA
    .......
    .......
    The formated file may be very popularly, but the module ConfigPaser
    doesn't handle it. Is there a way to process it freely?



  • bearophileHUGS@lycos.com

    #2
    Re: quickly read a formated file?

    lialie:
    The formated file may be very popularly, but the module ConfigPaser
    doesn't handle it. Is there a way to process it freely?
    First try, assuming the input file can be read whole. The code isn't
    much readable, it needs better variable names (name names?), comments,
    etc.

    data = """
    %HEADER
    title1 = "Untilted1"
    username = "User1"

    %DATA
    title2 = "Untilted2"
    username2 = "User2"
    """

    l1 = (p.strip().spli tlines() for p in data.split("%") if p.strip())
    result = {}
    for part in l1:
    pairs1 = (pair.split('=' ) for pair in part[1:])
    pairs2 = ((k.strip(), v.strip().strip ('"')) for k,v in pairs1)
    result[part[0]] = dict(pairs2)
    print result


    All done lazily for your better comfort :-)

    Bye,
    bearophile

    Comment

    • rzed

      #3
      Re: quickly read a formated file?

      bearophileHUGS@ lycos.com wrote in
      news:1172740062 .523000.209320@ 8g2000cwh.googl egroups.com:
      lialie:
      >The formated file may be very popularly, but the module
      >ConfigPaser doesn't handle it. Is there a way to process it
      >freely?
      >
      First try, assuming the input file can be read whole. The code
      isn't much readable, it needs better variable names (name
      names?), comments, etc.
      >
      data = """
      %HEADER
      title1 = "Untilted1"
      username = "User1"
      >
      %DATA
      title2 = "Untilted2"
      username2 = "User2"
      """
      >
      l1 = (p.strip().spli tlines() for p in data.split("%") if
      p.strip()) result = {}
      for part in l1:
      pairs1 = (pair.split('=' ) for pair in part[1:])
      pairs2 = ((k.strip(), v.strip().strip ('"')) for k,v in
      pairs1) result[part[0]] = dict(pairs2)
      print result
      >
      >
      If there could be embedded perecent signs in the data, that will
      produce some unexpected results, though. Here's another shot:

      data = """
      %HEADER
      title1 = "Untilted1"
      username = "User1"

      %DATA
      title2 = "The 7% Solution"
      username2 = "User2"
      """

      # Assumes there may be embedded percent signs in data
      # and all data lines are of the form key = value
      def parseData(data) :
      pd = {}
      idata = iter(data)
      for line in idata:
      line = line.strip()
      if line.startswith ('%'):
      tname = line[1:]
      cd = pd[tname] = {}
      line = idata.next().st rip()
      while line != '':
      if line.find('=') 0:
      id,val = line.split('=', 1)
      cd[id.strip()] = val.strip().str ip('"')
      line = idata.next().st rip()
      return pd


      print parseData(data. split('\n'))

      Comment

      • Jussi Salmela

        #4
        Re: quickly read a formated file?

        rzed kirjoitti:
        bearophileHUGS@ lycos.com wrote in
        news:1172740062 .523000.209320@ 8g2000cwh.googl egroups.com:
        >
        >lialie:
        >>The formated file may be very popularly, but the module
        >>ConfigPaser doesn't handle it. Is there a way to process it
        >>freely?
        >First try, assuming the input file can be read whole. The code
        >isn't much readable, it needs better variable names (name
        >names?), comments, etc.
        >>
        >data = """
        >%HEADER
        >title1 = "Untilted1"
        >username = "User1"
        >>
        >%DATA
        >title2 = "Untilted2"
        >username2 = "User2"
        >"""
        >>
        >l1 = (p.strip().spli tlines() for p in data.split("%") if
        >p.strip()) result = {}
        >for part in l1:
        > pairs1 = (pair.split('=' ) for pair in part[1:])
        > pairs2 = ((k.strip(), v.strip().strip ('"')) for k,v in
        pairs1) result[part[0]] = dict(pairs2)
        >print result
        >>
        >>
        >
        If there could be embedded perecent signs in the data, that will
        produce some unexpected results, though. Here's another shot:
        >
        >
        <snip>
        >
        >
        The solution of bearophile only needs 2 small modifications to handle this:

        #============== =============== ============
        data = """
        %HEADER
        title1 = "Untilted1"
        username = "User1 20 %"

        %DATA
        title2 = "Untilted2"
        username2 = "User2 10 %"
        """

        # Ensure data starts with a newline
        data = '\n' + data

        # Split using '\n%' instead of '%'
        l1 = (p.strip().spli tlines() for p in data.split("\n% ") if p.strip())
        result = {}
        for part in l1:
        pairs1 = (pair.split('=' ) for pair in part[1:])
        pairs2 = ((k.strip(), v.strip().strip ('"')) for k,v in pairs1)
        result[part[0]] = dict(pairs2)
        print result
        #============== =============== ============



        Cheers,
        Jussi

        Comment

        Working...