469,955 Members | 2,606 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,955 developers. It's quick & easy.

write Python dict (mb with unicode) to a file

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.
Jun 27 '08 #1
3 2002
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.
--
Jun 27 '08 #2
On 17:13, sabato 14 giugno 2008 dmitrey wrote:
hi all,
what's the best way to write Python dictionary to a file?
Pickle or ConfigParser.
You may gather more details at http://docs.python.org/lib/persistence.html

--
Mailsweeper Home : http://it.geocities.com/call_me_not_now/index.html
Jun 27 '08 #3
On Jun 14, 7:13 pm, dmitrey <dmitrey.kros...@scipy.orgwrote:
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
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Michael Weir | last post: by
1 post views Thread by 99miles | last post: by
1 post views Thread by Jaime Montes | last post: by
2 posts views Thread by starffly | last post: by
10 posts views Thread by could.net | last post: by
3 posts views Thread by =?ISO-2022-JP?B?Ik1hcnRpbiB2LiBMbyJ3aXMi?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.