473,382 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 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 2170
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)
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.