reading files into dicts

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

    #1

    reading files into dicts

    What's a good way to write a dictionary out to a file so that it can be
    easily read back into a dict later? I've used realines() to read text
    files into lists... how can I do the same thing with dicts? Here's some
    sample output that I'd like to write to file and then read back into a dict:

    {'.\\sync_pics. py': 1135900993, '.\\file_histor y.txt': 1135900994,
    '.\\New Text Document.txt': 1135900552}
  • Giovanni Bajo

    #2
    Re: reading files into dicts

    rbt wrote:
    [color=blue]
    > What's a good way to write a dictionary out to a file so that it can
    > be easily read back into a dict later? I've used realines() to read
    > text
    > files into lists... how can I do the same thing with dicts? Here's
    > some sample output that I'd like to write to file and then read back
    > into a dict:
    >
    > {'.\\sync_pics. py': 1135900993, '.\\file_histor y.txt': 1135900994,
    > '.\\New Text Document.txt': 1135900552}[/color]


    [color=blue][color=green][color=darkred]
    >>> d = {'.\\sync_pics. py': 1135900993, '.\\file_histor y.txt': 1135900994,[/color][/color][/color]
    .... '.\\New Text Document.txt': 1135900552}[color=blue][color=green][color=darkred]
    >>> file("foo", "w").write(repr (d))
    >>> data = file("foo").rea d()
    >>> data[/color][/color][/color]
    "{'.\\\\sync_pi cs.py': 1135900993, '.\\\\file_hist ory.txt': 1135900994,
    '.\\\\New Text Document.txt': 1135900552}"[color=blue][color=green][color=darkred]
    >>> d = eval(data)
    >>> d[/color][/color][/color]
    {'.\\sync_pics. py': 1135900993, '.\\file_histor y.txt': 1135900994, '.\\New Text
    Document.txt': 1135900552}

    --
    Giovanni Bajo


    Comment

    • limodou

      #3
      Re: reading files into dicts

      2005/12/30, rbt <rbt@athop1.ath .vt.edu>:[color=blue]
      > What's a good way to write a dictionary out to a file so that it can be
      > easily read back into a dict later? I've used realines() to read text
      > files into lists... how can I do the same thing with dicts? Here's some
      > sample output that I'd like to write to file and then read back into a dict:
      >
      > {'.\\sync_pics. py': 1135900993, '.\\file_histor y.txt': 1135900994,
      > '.\\New Text Document.txt': 1135900552}
      > --
      > http://mail.python.org/mailman/listinfo/python-list
      >[/color]

      You can try the dict4ini module written by me.



      The dict can be saved as an Ini file, and can be read back from the
      Ini file.Just like:
      [color=blue][color=green][color=darkred]
      >>> import dict4ini
      >>> x = {'.\\sync_pics. py': 1135900993, '.\\file_histor y.txt':[/color][/color][/color]
      1135900994, '.\\New Text Document.txt': 1135900552}[color=blue][color=green][color=darkred]
      >>> d = dict4ini.DictIn i(values=x)
      >>> d['.\sync_pics.py '][/color][/color][/color]
      1135900993

      --
      I like python!
      My Blog: http://www.donews.net/limodou
      NewEdit Maillist: http://groups.google.com/group/NewEdit

      Comment

      • Gary Herron

        #4
        Re: reading files into dicts

        rbt wrote:
        [color=blue]
        >What's a good way to write a dictionary out to a file so that it can be
        >easily read back into a dict later? I've used realines() to read text
        >files into lists... how can I do the same thing with dicts? Here's some
        >sample output that I'd like to write to file and then read back into a dict:
        >
        >{'.\\sync_pics .py': 1135900993, '.\\file_histor y.txt': 1135900994,
        >'.\\New Text Document.txt': 1135900552}
        >
        >[/color]


        A better way, than rolling your own marshaling (as this is called),
        would be to use the cPickle module. It can write almost any Python
        object to a file, and then read it back in later. It's more efficient,
        and way more general than any code you're likely to write yourself.

        The contents of the file are quite opaque to anything except the cPickle
        and pickle modules. If you *do* want to roll you own input and output to
        the file, the standard lib functions "repr" and "eval" can be used. Repr
        is meant to write out objects so they can be read back in and recovered
        with eval. If the contents of your dictionary are well behaved enough
        (simple Python objects are, and classes you create may be made so), then
        you may be able to get away with as little as this:

        f = file('file.name ', 'wb')
        f.write(repr(my Dictionary))
        f.close()

        and

        f = file('file.name ', 'rb')
        myDictionary = eval(f.read())
        f.close()

        Simple as that is, I'd still recommend the cPickle module.

        As always, this security warning applys: Evaluating arbitrary text
        allows anyone, who can change that text, to take over complete control
        of your program. So be carefully.

        Gary Herron


        Comment

        • Tim Williams (gmail)

          #5
          Re: reading files into dicts

          A further thought, if you write the data to a file in the correct
          format, you can use import and reload to access the data later instead
          of repr.





          On 12/29/05, Tim Williams (gmail) <tdwdotnet@gmai l.com> wrote:[color=blue]
          > On 30/12/05, Giovanni Bajo <noway@sorry.co m> wrote:[color=green]
          > >
          > >[color=darkred]
          > > >>> d = {'.\\sync_pics. py': 1135900993, '.\\file_histor y.txt':[/color][/color]
          > 1135900994,[color=green]
          > > .... '.\\New Text Document.txt': 1135900552}[color=darkred]
          > > >>> file("foo", "w").write(repr (d))
          > > >>> data = file("foo").rea d()
          > > >>> data[/color]
          > > "{'.\\\\sync_pi cs.py': 1135900993, '.\\\\file_hist ory.txt': 1135900994,
          > > '.\\\\New Text Document.txt': 1135900552}"[color=darkred]
          > > >>> d = eval(data)
          > > >>> d[/color]
          > > {'.\\sync_pics. py': 1135900993, '.\\file_histor y.txt': 1135900994,[/color]
          > '.\\New[color=green]
          > > Text
          > > Document.txt': 1135900552}
          > >
          > > eval() is risky if you can't control the contents of the file :)[/color]
          >
          >[/color]


          --

          Tim Williams

          Comment

          • Tim Williams (gmail)

            #6
            Re: reading files into dicts

            Apologies for the top post, it was my first attempt at using gmail's
            pda-enabled web interface. There is no option to bottom post.

            Apart from the mistake in my previous reply, when I meant to suggest
            using import instead of eval() not repr(). I also omitted an example.
            So here goes -but I don't know how gmail-CE will format this!!

            Save your dict to a file with a .PY extension using

            outdata = 'mydict =' + repr(dict) # or somesuch
            similar

            dictfile.py
            -----------------------
            mydict = {'key1':'a', 'key2':'b'}
            -----------------------

            Then:

            import dictfile
            print dictfile.mydict .keys()[color=blue][color=green][color=darkred]
            >>>['key1','key2'][/color][/color][/color]
            reload(dictfile )

            HTH :-)

            --

            Tim Williams

            Comment

            • rbt

              #7
              Re: reading files into dicts

              Gary Herron wrote:[color=blue]
              > rbt wrote:
              >[color=green]
              >> What's a good way to write a dictionary out to a file so that it can
              >> be easily read back into a dict later? I've used realines() to read
              >> text files into lists... how can I do the same thing with dicts?
              >> Here's some sample output that I'd like to write to file and then read
              >> back into a dict:
              >>
              >> {'.\\sync_pics. py': 1135900993, '.\\file_histor y.txt': 1135900994,
              >> '.\\New Text Document.txt': 1135900552}
              >>
              >>[/color]
              >
              >
              > A better way, than rolling your own marshaling (as this is called),
              > would be to use the cPickle module. It can write almost any Python
              > object to a file, and then read it back in later. It's more efficient,
              > and way more general than any code you're likely to write yourself.
              >
              > The contents of the file are quite opaque to anything except the cPickle
              > and pickle modules. If you *do* want to roll you own input and output to
              > the file, the standard lib functions "repr" and "eval" can be used. Repr
              > is meant to write out objects so they can be read back in and recovered
              > with eval. If the contents of your dictionary are well behaved enough
              > (simple Python objects are, and classes you create may be made so), then
              > you may be able to get away with as little as this:
              >
              > f = file('file.name ', 'wb')
              > f.write(repr(my Dictionary))
              > f.close()
              >
              > and
              >
              > f = file('file.name ', 'rb')
              > myDictionary = eval(f.read())
              > f.close()
              >
              > Simple as that is, I'd still recommend the cPickle module.
              >
              > As always, this security warning applys: Evaluating arbitrary text
              > allows anyone, who can change that text, to take over complete control
              > of your program. So be carefully.
              >
              > Gary Herron
              >
              >[/color]

              Thanks to everyone for the tips on eval and repr. I went with the
              cPickle suggestion... this is awesome! It was the easiest and quickest
              solution performance-wise. Just makes me think, "Wow... how the heck
              does pickle do that?!"

              Thanks again,
              rbt

              Comment

              • Chris F.A. Johnson

                #8
                Re: reading files into dicts

                On 2005-12-30, Tim Williams (gmail) wrote:[color=blue]
                > Apologies for the top post, it was my first attempt at using gmail's
                > pda-enabled web interface. There is no option to bottom post.[/color]

                Can you not move the cursor?

                --
                Chris F.A. Johnson, author | <http://cfaj.freeshell. org>
                Shell Scripting Recipes: | My code in this post, if any,
                A Problem-Solution Approach | is released under the
                2005, Apress | GNU General Public Licence

                Comment

                • Fuzzyman

                  #9
                  Re: reading files into dicts

                  ConfigObj is good - it (effectively) turns a dictionary into an ini
                  file, and vice versa.

                  There is also built in support for type conversion.

                  See http://www.voidspace.org.uk/python/configobj.html

                  See the ConfigPersist module which has functions to use ConfigObj for
                  data persistence. It explains the limitations.

                  http://www.voidspace.org.uk/python/configpersist.html

                  Basically you can store and retrieve dictionaries, lists, strings,
                  integers, floats and booleans. You can nest dictionaries - but you
                  can't nest dictionaries in lists. All the keys must be strings - but
                  the module/article suggests a way round that, at the expense of
                  readability of the resulting text file.

                  All the best,

                  Fuzzyman
                  http://www.voidspace.org.uk/python/index.shtml

                  Comment

                  • Alex Martelli

                    #10
                    Re: reading files into dicts

                    rbt <rbt@athop1.ath .vt.edu> wrote:
                    ...[color=blue]
                    > Thanks to everyone for the tips on eval and repr. I went with the
                    > cPickle suggestion... this is awesome! It was the easiest and quickest
                    > solution performance-wise. Just makes me think, "Wow... how the heck
                    > does pickle do that?!"[/color]

                    pickle.py implements just the same job, in pure Python, and it's easily
                    found within your Python's standard library, so you may want to study it
                    to see how it does perform its task.


                    Alex

                    Comment

                    Working...