write Python dict (mb with unicode) to a file

Discussion in 'Python' started by dmitrey, Jun 14, 2008.

  1. dmitrey

    dmitrey Guest

    hi all,
    what's the best way to write Python dictionary to a file?

    (and then read)

    There could be unicode field names and values encountered.
    Thank you in advance, D.
     
    dmitrey, Jun 14, 2008
    #1
    1. Advertising

  2. dmitrey

    TheSaint Guest

    TheSaint, Jun 14, 2008
    #2
    1. Advertising

  3. dmitrey wrote:
    > hi all,
    > what's the best way to write Python dictionary to a file?
    >
    > (and then read)
    >
    > There could be unicode field names and values encountered.
    > Thank you in advance, D.


    pickle/cPickle, perhaps, if you're willing to trust the file (since it's
    basically eval()ed)? Or JSON (use simplejson or the enhanced version of
    cjson), though I doubt it would be super-fast.
    --
     
    Matt Nordhoff, Jun 14, 2008
    #3
  4. dmitrey

    John Machin Guest

    On Jun 14, 7:13 pm, dmitrey <> wrote:
    > hi all,
    > what's the best way to write Python dictionary to a file?
    >
    > (and then read)
    >
    > There could be unicode field names and values encountered.


    I'm presuming that "field names" means "dictionary keys". If not
    unicode, are the remainder of the keys and values: strings encoded in
    ASCII? strings encoded otherwise? neither str nor unicode?


    > Thank you in advance, D.


    "Best" depends on how you measure it.

    cPickle is one alternative (ensure you use protocol=-1). Speed should
    be OK, but format not documented AFAIK other than in the source code,
    so not readable outside the Python universe. Also it won't matter what
    types of data you have.

    A portable alternative (and simple enough if all your data are str/
    unicode) would be to encode all your strings as UTF-8, and then write
    the key/value pairs out to a csv file:
    # untested pseudocode for basestring-only case:
    for k, v in mydict.iteritems():
    csv_writer.writerow((k.encode('utf8'), v.encode('utf8')))
    # if you have str instances encoded other than in ASCII or your
    system's default encoding, you'll have to work a bit harder ...

    Cheers,
    John
     
    John Machin, Jun 14, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ethan Furman

    Python 3: dict & dict.keys()

    Ethan Furman, Jul 24, 2013, in forum: Python
    Replies:
    4
    Views:
    272
    Steven D'Aprano
    Jul 25, 2013
  2. Peter Otten

    Re: Python 3: dict & dict.keys()

    Peter Otten, Jul 24, 2013, in forum: Python
    Replies:
    1
    Views:
    113
    Neil Cerutti
    Jul 24, 2013
  3. Oscar Benjamin

    Re: Python 3: dict & dict.keys()

    Oscar Benjamin, Jul 24, 2013, in forum: Python
    Replies:
    0
    Views:
    124
    Oscar Benjamin
    Jul 24, 2013
  4. Peter Otten

    Re: Python 3: dict & dict.keys()

    Peter Otten, Jul 24, 2013, in forum: Python
    Replies:
    0
    Views:
    105
    Peter Otten
    Jul 24, 2013
  5. Skip Montanaro

    Re: Python 3: dict & dict.keys()

    Skip Montanaro, Jul 24, 2013, in forum: Python
    Replies:
    0
    Views:
    116
    Skip Montanaro
    Jul 24, 2013
Loading...

Share This Page