File to dict

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

    #16
    Re: File to dict

    On 2007-12-07, Bruno Desthuilliers <bruno.42.desth uilliers@wtf.we bsiteburo.oops. comwrote:
    mrkafk@gmail.co m a écrit :
    >>
    >>The csv module is your friend.
    >>
    >(slapping forehead) why the Holy Grail didn't I think about this?
    >
    If that can make you feel better, a few years ago, I spent two
    days writing my own (SquaredWheel(t m) of course) csv
    reader/writer... before realizing there was such a thing as the
    csv module :-/
    >
    Should have known better...
    But probably it has made you a better person. ;)

    --
    Neil Cerutti

    Comment

    • Duncan Booth

      #17
      Re: File to dict

      Neil Cerutti <horpner@yahoo. comwrote:
      On 2007-12-07, Duncan Booth <duncan.booth@i nvalid.invalidw rote:
      >from __future__ import with_statement
      >>
      >def loaddomainowner s(domain):
      > with open('/etc/virtual/domainowners',' r') as infile:
      >
      I've been thinking I have to use contextlib.clos ing for
      auto-closing files. Is that not so?
      >
      That is not so.

      Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit
      (Intel)] on win32
      Type "help", "copyright" , "credits" or "license" for more information.
      >>from __future__ import with_statement
      >>with open('diffs.txt ') as f:
      .... print len(list(f))
      ....
      40
      >>f
      <closed file 'diffs.txt', mode 'r' at 0x00AA0698>
      >>>

      Comment

      • Glauco

        #18
        Re: File to dict

        mrkafk@gmail.co m ha scritto:
        Hello everyone,
        >
        I have written this small utility function for transforming legacy
        file to Python dict:
        >
        >
        def lookupdmo(domai n):
        lines = open('/etc/virtual/domainowners',' r').readlines()
        lines = [ [y.lstrip().rstr ip() for y in x.split(':')] for x in
        lines]
        lines = [ x for x in lines if len(x) == 2 ]
        d = dict()
        for line in lines:
        d[line[0]]=line[1]
        return d[domain]
        >
        cache = None

        def lookup( domain ):
        if not cache:
        cache = dict( [map( lambda x: x.strip(), x.split(':')) for x in
        open('/etc/virtual/domainowners',' r').readlines()])
        return cache.get(domai n)





        Glauco

        Comment

        • Neil Cerutti

          #19
          Re: File to dict

          On 2007-12-07, Duncan Booth <duncan.booth@i nvalid.invalidw rote:
          Neil Cerutti <horpner@yahoo. comwrote:
          >
          >On 2007-12-07, Duncan Booth <duncan.booth@i nvalid.invalidw rote:
          >>from __future__ import with_statement
          >>>
          >>def loaddomainowner s(domain):
          >> with open('/etc/virtual/domainowners',' r') as infile:
          >>
          >I've been thinking I have to use contextlib.clos ing for
          >auto-closing files. Is that not so?
          >>
          That is not so.
          >
          Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit
          (Intel)] on win32
          Type "help", "copyright" , "credits" or "license" for more information.
          >>>from __future__ import with_statement
          >>>with open('diffs.txt ') as f:
          ... print len(list(f))
          ...
          40
          >>>f
          ><closed file 'diffs.txt', mode 'r' at 0x00AA0698>
          Thanks. After seeing your answer I managed to find what I'd
          overlooked before, in the docs for file.close:

          As of Python 2.5, you can avoid having to call this method
          explicitly if you use the with statement. For example, the
          following code will automatically close f when the with block
          is exited:

          from __future__ import with_statement

          with open("hello.txt ") as f:
          for line in f:
          print line

          --
          Neil Cerutti

          Comment

          • david

            #20
            Re: File to dict

            On Fri, 07 Dec 2007 16:46:56 +0100, Glauco wrote:
            mrkafk@gmail.co m ha scritto:
            >Hello everyone,
            >>
            >I have written this small utility function for transforming legacy file
            >to Python dict:
            >>
            >>
            >def lookupdmo(domai n):
            > lines = open('/etc/virtual/domainowners',' r').readlines() lines
            > = [ [y.lstrip().rstr ip() for y in x.split(':')] for x in
            >lines]
            > lines = [ x for x in lines if len(x) == 2 ] d = dict()
            > for line in lines:
            > d[line[0]]=line[1]
            > return d[domain]
            >>
            >>
            cache = None
            >
            def lookup( domain ):
            if not cache:
            cache = dict( [map( lambda x: x.strip(), x.split(':')) for x in
            open('/etc/virtual/domainowners',' r').readlines()])
            return cache.get(domai n)
            >
            >>lookup(1)
            Traceback (most recent call last):
            File "<stdin>", line 1, in <module>
            File "<stdin>", line 2, in lookup
            UnboundLocalErr or: local variable 'cache' referenced before assignment

            You miss the:
            def lookup(domain):
            global cache
            ...

            bye

            Comment

            • Glauco

              #21
              Re: File to dict

              david ha scritto:
              On Fri, 07 Dec 2007 16:46:56 +0100, Glauco wrote:
              >
              >mrkafk@gmail.co m ha scritto:
              >>Hello everyone,
              >>>
              >>I have written this small utility function for transforming legacy file
              >>to Python dict:
              >>>
              >>>
              >>def lookupdmo(domai n):
              >> lines = open('/etc/virtual/domainowners',' r').readlines() lines
              >> = [ [y.lstrip().rstr ip() for y in x.split(':')] for x in
              >>lines]
              >> lines = [ x for x in lines if len(x) == 2 ] d = dict()
              >> for line in lines:
              >> d[line[0]]=line[1]
              >> return d[domain]
              >>>
              >>>
              >cache = None
              >>
              >def lookup( domain ):
              > if not cache:
              > cache = dict( [map( lambda x: x.strip(), x.split(':')) for x in
              >open('/etc/virtual/domainowners',' r').readlines()])
              > return cache.get(domai n)
              >>
              >
              >>>lookup(1)
              Traceback (most recent call last):
              File "<stdin>", line 1, in <module>
              File "<stdin>", line 2, in lookup
              UnboundLocalErr or: local variable 'cache' referenced before assignment
              >
              You miss the:
              def lookup(domain):
              global cache
              ...
              >
              bye
              yezzz!

              you can use global or static


              Gla

              Comment

              • mrkafk@gmail.com

                #22
                Re: File to dict



                Glauco wrote:
                cache = None
                >
                def lookup( domain ):
                if not cache:
                cache = dict( [map( lambda x: x.strip(), x.split(':')) for x in
                open('/etc/virtual/domainowners',' r').readlines()])
                return cache.get(domai n)
                Neat solution! It just needs small correction for empty or badly
                formed lines:

                dict([map( lambda x: x.strip(), x.split(':')) for x in open('/etc/
                virtual/domainowners',' r') if ':' in x])

                Comment

                • Jeremy C B Nicoll

                  #23
                  Re: File to dict

                  Chris <cwitts@gmail.c omwrote:
                  For the first one you are parsing the entire file everytime you want
                  to lookup just one domain...
                  Is the file sorted? If so wouldn't it be easier either to read the whole
                  thing and then binary-chop search it, or if the file is vast to use seek
                  creatively to binary chop it but only read particular records from disk?

                  (One could chop by using seek to locate to a particular byte location in the
                  file then read to end of that record then read the next complete record.)

                  If the file isn't sorted, then .... why not? Also if the file is vast
                  surely there's some or a lot of point in breaking it up into a group of
                  smaller files?

                  --
                  Jeremy C B Nicoll - my opinions are my own.

                  Comment

                  Working...