473,385 Members | 1,341 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,385 software developers and data experts.

save dictionary for later use?

i extract info from one file and put it into a dictionary.
i want to save that dictionary for later use, how do i do that?
might save a list of dictionaries or a list of classobjects too if
there is any difference.
Jun 27 '08 #1
6 4584
On May 16, 2:17 pm, globalrev <skanem...@yahoo.sewrote:
i extract info from one file and put it into a dictionary.
i want to save that dictionary for later use, how do i do that?
might save a list of dictionaries or a list of classobjects too if
there is any difference.
use the 'pickle' module.
http://docs.python.org/lib/module-pickle.html

....
Jay Graves
Jun 27 '08 #2
On 16 Maj, 21:22, jay graves <jaywgra...@gmail.comwrote:
On May 16, 2:17 pm, globalrev <skanem...@yahoo.sewrote:
i extract info from one file and put it into a dictionary.
i want to save that dictionary for later use, how do i do that?
might save a list of dictionaries or a list of classobjects too if
there is any difference.

use the 'pickle' module.http://docs.python.org/lib/module-pickle.html

...
Jay Graves

pickle.dumps(mg)
pickle.load(mg)

'dict' object has no attribute 'readline'
dumps load(well i dont know but i get no complaint but running load
generates that error)
Jun 27 '08 #3
On May 16, 3:24 pm, globalrev <skanem...@yahoo.sewrote:
On 16 Maj, 21:22, jay graves <jaywgra...@gmail.comwrote:
On May 16, 2:17 pm, globalrev <skanem...@yahoo.sewrote:
i extract info from one file and put it into a dictionary.
i want to save that dictionary for later use, how do i do that?
might save a list of dictionaries or a list of classobjects too if
there is any difference.
use the 'pickle' module.http://docs.python.org/lib/module-pickle.html
pickle.dumps(mg)
pickle.load(mg)

'dict' object has no attribute 'readline'
dumps load(well i dont know but i get no complaint but running load
generates that error)
It's best to post a minimal set of code that exhibits your error.
You aren't saving the output of pickle.dumps and you are using
pickle.load instead of pickle.loads.

Sample loading to and from a string which you can tuck away in a file.
>>import pickle
test = {'a':1,'b':2}
picklestr = pickle.dumps(test)
test2 = pickle.loads(picklestr)
test == test2
True
>>>

Sample using an open file.
>>import pickle
test = {'a':1,'b':2}
pfile = open('pickletest','wb')
pickle.dump(test,pfile)
pfile.close()
pfile = open('pickletest','rb')
test2 = pickle.load(pfile)
pfile.close()
test == test2
True
>>>
....
Jay Graves
Jun 27 '08 #4
globalrev wrote:
pickle.dumps(mg)
pickle.load(mg)

'dict' object has no attribute 'readline'
dumps load(well i dont know but i get no complaint but running load
generates that error)
The 'loads' and 'dumps' methods use strings:
>>import pickle
d = {"this": 42, "that": 101, "other": 17}
s = pickle.dumps(d)
s
"(dp0\nS'this'\np1\nI42\nsS'other'\np2\nI17\nsS'th at'\np3\nI101\ns."
>>pickle.loads(s)
{'this': 42, 'other': 17, 'that': 101}

If you want to store to / restore from file, use 'dump' and 'load':

# write to file 'out'...
>>f = open("out")
f = open("out", "wb")
pickle.dump(d, f)
f.close()
# restore it later
>>g = open("out", "rb")
e = pickle.load(g)
g.close()
e
{'this': 42, 'other': 17, 'that': 101}

Also see http://docs.python.org/lib/pickle-example.html.

Hope this helps!

--Hans
Jun 27 '08 #5
On May 16, 4:23*pm, Hans Nowak <zephyrfalcon!NO_SP...@gmail.com>
wrote:
globalrev wrote:
pickle.dumps(mg)
pickle.load(mg)
'dict' object has no attribute 'readline'
dumps load(well i dont know but i get no complaint but running load
generates that error)

The 'loads' and 'dumps' methods use strings:

*>>import pickle
*>>d = {"this": 42, "that": 101, "other": 17}
*>>s = pickle.dumps(d)
*>>s
"(dp0\nS'this'\np1\nI42\nsS'other'\np2\nI17\nsS'th at'\np3\nI101\ns."
*>>pickle.loads(s)
{'this': 42, 'other': 17, 'that': 101}

If you want to store to / restore from file, use 'dump' and 'load':

# write to file 'out'...
*>>f = open("out")
*>>f = open("out", "wb")
*>>pickle.dump(d, f)
*>>f.close()

# restore it later
*>>g = open("out", "rb")
*>>e = pickle.load(g)
*>>g.close()
*>>e
{'this': 42, 'other': 17, 'that': 101}

Also seehttp://docs.python.org/lib/pickle-example.html.

Hope this helps!

--Hans
I want to compare that cleanliness with other languages to compare
formats.

Is pickle.load( open( 'out', 'rb' ) ) any better or worse than
pickle.load( 'out', 'rb' )?
Jun 27 '08 #6
On May 17, 3:52*am, castironpi <castiro...@gmail.comwrote:
On May 16, 4:23*pm, Hans Nowak <zephyrfalcon!NO_SP...@gmail.com>
wrote:


globalrev wrote:
pickle.dumps(mg)
pickle.load(mg)
'dict' object has no attribute 'readline'
dumps load(well i dont know but i get no complaint but running load
generates that error)
The 'loads' and 'dumps' methods use strings:
*>>import pickle
*>>d = {"this": 42, "that": 101, "other": 17}
*>>s = pickle.dumps(d)
*>>s
"(dp0\nS'this'\np1\nI42\nsS'other'\np2\nI17\nsS'th at'\np3\nI101\ns."
*>>pickle.loads(s)
{'this': 42, 'other': 17, 'that': 101}
If you want to store to / restore from file, use 'dump' and 'load':
# write to file 'out'...
*>>f = open("out")
*>>f = open("out", "wb")
*>>pickle.dump(d, f)
*>>f.close()
# restore it later
*>>g = open("out", "rb")
*>>e = pickle.load(g)
*>>g.close()
*>>e
{'this': 42, 'other': 17, 'that': 101}
Also seehttp://docs.python.org/lib/pickle-example.html.
Hope this helps!
--Hans

I want to compare that cleanliness with other languages to compare
formats.

Is pickle.load( open( 'out', 'rb' ) ) any better or worse than
pickle.load( 'out', 'rb' )?- Hide quoted text -

- Show quoted text -
This is a check-in on live-time writing. pickle.load didn't take two
parameters.
Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: John Mudd | last post by:
I must be missing something here. It's clearly faster to lookup an item directly in a dictionary than to scan through a list. So when I have a large lookup table I always load it in the form of a...
10
by: Luis P. Mendes | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, my program builds a dictionary that I would like to save in a file. My question is what are the simple ways to do it? The first solution...
3
by: py | last post by:
Say I have classes which represent parts of a car such as Engine, Body, etc. Now I want to represent a Car in a nested dictionary like... {string_id:{engine_id:engine_object,...
0
by: ssg31415926 | last post by:
I've been trying to save a hashtable in an Application Settings file. I need to save settings for each tabPage on a form. Trouble is, the number of tabPages is determined at runtime, so I can't...
14
by: vatamane | last post by:
This has been bothering me for a while. Just want to find out if it just me or perhaps others have thought of this too: Why shouldn't the keyset of a dictionary be represented as a set instead of a...
4
by: NullQwerty | last post by:
Hi folks, I have a Dictionary which contains a string key and an object value. I want the object value to point to a property in my class and I want it to be by reference, so that later on I...
11
by: John | last post by:
I am coding a radix sort in python and I think that Python's dictionary may be a choice for bucket. The only problem is that dictionary is a mapping without order. But I just found that if the...
9
by: nik | last post by:
Hi, I would like to create a class and then save it for re-use later. I have tried to use pickle, but am not sure if that is right. I am sorry, but I am new to python. Basically, I have a...
6
by: GiJeet | last post by:
hello, I'm trying to use a dictionary as a class member. I want to use a property to get/set the key/value of the dictionary but I'm confused as how to use a dictionary as a property. Since there...
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?

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.