472,782 Members | 1,184 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,782 software developers and data experts.

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 2137
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Michael Weir | last post by:
I'm sure this is a very simple thing to do, once you know how to do it, but I am having no fun at all trying to write utf-8 strings to a unicode file. Does anyone have a couple of lines of code...
1
by: 99miles | last post by:
Hello- I can't figure out how to create and write to a Unicode file using C++ in Visual Studio 6.0. I need to do this for windows only. Could someonw please help!??? thanks-
3
by: Mark Miller | last post by:
I have a char array and when I write it to a file using BinaryWriter the position of the pointer is the size of the array + 1. For example: writing char leaves the pointer at position 26 after...
1
by: Jaime Montes | last post by:
I have found that adding in the start of the file the character '-1' and '-2' I can read the file as a Unicode, and to write any character I have to write pairs of character so for 'a' I write '0'...
2
by: hezhenjie | last post by:
Hi, all: I just need to parse a unicode file, and assume to get data one line by one line. I use _wfopen(), fgetws(), wcslen(), wcsstr(), making it work normally on Windows platform. However,...
2
by: starffly | last post by:
I want to read a xml file in Unicode, UTF-8 or a native encoding into a wchar_t type string, so i write a routine as follows, however, sometimes a Unicode file including Chinese character cannot...
10
by: could.net | last post by:
Python dict is a hash table, isn't it? I know that hashtable has the concept of "bucket size" and "min bucket count" stuff, and they should be configurable so I can set them to the proper value...
3
by: =?ISO-2022-JP?B?Ik1hcnRpbiB2LiBMbyJ3aXMi?= | last post by:
Step 4: Either wait for Python 2.7 or apply the patch to your own copy Actually, this is released in Python 2.6, see r62724. Regards, Martin
0
by: danielo | last post by:
i have a python function like this : def func1(**fields) : ... fields is a python dict, in python i can send the following to the function : c = dict(a="3",b="4",f="3") func1(**c)
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.