473,583 Members | 4,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4596
On May 16, 2:17 pm, globalrev <skanem...@yaho o.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...@gma il.comwrote:
On May 16, 2:17 pm, globalrev <skanem...@yaho o.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...@yaho o.sewrote:
On 16 Maj, 21:22, jay graves <jaywgra...@gma il.comwrote:
On May 16, 2:17 pm, globalrev <skanem...@yaho o.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(te st)
test2 = pickle.loads(pi cklestr)
test == test2
True
>>>

Sample using an open file.
>>import pickle
test = {'a':1,'b':2}
pfile = open('pickletes t','wb')
pickle.dump(t est,pfile)
pfile.close ()
pfile = open('pickletes t','rb')
test2 = pickle.load(pfi le)
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'ot her'\np2\nI17\n sS'that'\np3\nI 101\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!N O_SP...@gmail.c om>
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'ot her'\np2\nI17\n sS'that'\np3\nI 101\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...@gma il.comwrote:
On May 16, 4:23*pm, Hans Nowak <zephyrfalcon!N O_SP...@gmail.c om>
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'ot her'\np2\nI17\n sS'that'\np3\nI 101\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
6577
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 dictionary. But it seems a waste. I end up having to assign an artificial value to the dictionary entry. Below I assign the value "None" to each...
10
6952
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 I've thought of is to transform the dictionary in a
3
1153
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, body_id:body_object}}....ok? Well the other thing is that I am allowed to store strings in this dictionary...so I can't just store the Engine and Body object and...
0
3589
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 have individual settings for each page. I decided to hold them in a collection. I started out with a generic Dictionary but that didn't work. So,...
14
3448
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 list? I know that sets were introduced a lot later and lists/dictionaries were used instead but I think "the only correct way" now is for the...
4
14692
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 can change the value of the property through the dictionary. I am having difficulty making the value be by reference. Is this possible? I've even...
11
11396
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 keys are numeric, the keys themselves are ordered in the dictionary. part of my code is like this: radix={} for i in range(256):
9
3076
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 class, Map. I want to be able to create new maps: MapA, MapB... that have Map as the base class. start with-
6
10166
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 are 2 parts, I don't know how to setup the get/sets. I tried searching but could not find any examples. I'd appreciate an example of how to get/...
0
7825
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7933
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8191
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5372
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3841
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1431
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1155
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.