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. 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
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)
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
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
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' )?
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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
|
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...
|
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,...
|
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...
| |
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...
|
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):
|
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-
|
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/...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |