Good day,
The purpose of my script is to create a small dictionary that is saved to a file using pickle and print the entire contents of the dictionary for the user. My script prompts the user to make 1 of 3 choices:
Selection 1 is intended to print all contents of a dictionary loaded using pickle. Selection 2 appends to the existing dictionary and prints what was appended. Selection 3 creates a file and adds an entry into it.
When I select option 1, only the first entry prints.
When I use select option 2, I can add an entry and I can see that entry in the file that is in the same folder as the script. I am unsure what type of file it is. I can open with notepad though. The new entry also prints.
I only select option 3 once, to create the file. It adds the first entry and prints it successfully.
I need help getting the entire contents of the dictionary to print. -
import pickle
-
-
-
print("""\n
-
1- View codes
-
2- Add code
-
3- Create file/add first""")
-
-
while True:
-
choice = input("What is your choice?")
-
-
DC_Message = {}
-
-
-
if choice == "1":
-
filename = "dcoutput"
-
infile = open(filename, 'rb')
-
DC_Message2 = pickle.load(infile)
-
infile.close()
-
for key, value in DC_Message2.items():
-
print(key, ' : ', value)
-
-
-
elif choice == "2":
-
DCcode = str(input("What is the new DCcode?"))
-
Message = str(input("What is the new message"))
-
DC_Message[DCcode] = Message
-
print(DC_Message)
-
for key, value in DC_Message.items():
-
outfile = open("dcoutput", 'ab')
-
pickle.dump(DC_Message, outfile)
-
outfile.close()
-
print(key, ' : ', value)
-
-
elif choice == "3":
-
DCcode = str(input("What is the new DCcode?"))
-
Message = str(input("What is the new message"))
-
DC_Message[DCcode] = Message
-
print(DC_Message)
-
for key, value in DC_Message.items():
-
outfile = open("dcoutput", 'wb')
-
pickle.dump(DC_Message, outfile)
-
outfile.close()
-
print(key, ' : ', value)
1 11208 dev7060 644
Recognized Expert Contributor
The purpose of my script is to create a small dictionary that is saved to a file using pickle and print the entire contents of the dictionary for the user. My script prompts the user to make 1 of 3 choices:
Selection 1 is intended to print all contents of a dictionary loaded using pickle. Selection 2 appends to the existing dictionary and prints what was appended. Selection 3 creates a file and adds an entry into it.
When I select option 1, only the first entry prints.
When I use select option 2, I can add an entry and I can see that entry in the file that is in the same folder as the script. I am unsure what type of file it is. I can open with notepad though. The new entry also prints.
I only select option 3 once, to create the file. It adds the first entry and prints it successfully.
I need help getting the entire contents of the dictionary to print. - import pickle
-
-
-
print("""\n
-
1- View codes
-
2- Add code
-
3- Create file/add first""")
-
-
while True:
-
choice = input("What is your choice?")
-
-
DC_Message = {}
-
-
-
if choice == "1":
-
filename = "dcoutput"
-
infile = open(filename, 'rb')
-
DC_Message2 = pickle.load(infile)
-
infile.close()
-
for key, value in DC_Message2.items():
-
print(key, ' : ', value)
-
-
-
elif choice == "2":
-
DCcode = str(input("What is the new DCcode?"))
-
Message = str(input("What is the new message"))
-
DC_Message[DCcode] = Message
-
print(DC_Message)
-
for key, value in DC_Message.items():
-
outfile = open("dcoutput", 'ab')
-
pickle.dump(DC_Message, outfile)
-
outfile.close()
-
print(key, ' : ', value)
-
-
elif choice == "3":
-
DCcode = str(input("What is the new DCcode?"))
-
Message = str(input("What is the new message"))
-
DC_Message[DCcode] = Message
-
print(DC_Message)
-
for key, value in DC_Message.items():
-
outfile = open("dcoutput", 'wb')
-
pickle.dump(DC_Message, outfile)
-
outfile.close()
-
print(key, ' : ', value)
pickle.load(inf ile) unserializes one object at a time. The file pointer is then shifted to the beginning of the next object. You may use it like below to unserialize all the objects. - while True:
-
try:
-
print(pickle.load(infile))
-
except EOFError:
-
break
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: none |
last post by:
or is it just me?
I am having a problem with using a dictionary as an attribute of a
class. This happens in python 1.5.2 and 2.2.2 which I am accessing
through pythonwin builds 150 and 148 respectively
In the sample code you see that I have class Item and class Dict
class Dict contains a dictionary called items. The items dictionary
will contain instances of Item that are keyed off of the Item name. In
__main__ I create two...
|
by: Jesse Bloom |
last post by:
I keep running into a problem when I use pickle or cPickle to unpickle a
python object I have serialized from the database. I read the string
for the serialized object. Apparently I am missing an argument? Here's
what happens:
cPickle.loads(s)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: __new__() takes exactly 2 arguments (1 given)
|
by: Alan Silver |
last post by:
Hello,
I have a server running Windows Server 2003, on which two of the web
sites use the MegaBBS ASP forum software. Both sites suddenly developed
the same error, which seems to be connected to the dictionary object.
After some tinkering, I whittled it down to the following (complete)
ASP...
<%@ CodePage=65001 Language="VBScript"%>
|
by: News |
last post by:
Hi Everyone,
The attached code creates client connections to websphere queue managers
and then processes an inquiry against them.
The program functions when it gets options from the command line.
It also works when pulling the options from a file.
|
by: Bo Peng |
last post by:
Dear list,
I have been using mingw to build a python extension module. I had to
jump through a number of hooks like the _ctype problem caused by the use
of msvcr71.dll, but the module was mostly usable.
Things become complicated when I use more and more boost libraries and
mingw can not work well with some of the modules. I am trying to switch
to VC but I was suggested that I need to use the identical version of VC
that is used to...
| |
by: erikcw |
last post by:
Hi,
I'm trying to turn o list of objects into a dictionary using a list
comprehension.
Something like
entries = {}
= d.id] for d in links]
|
by: neptundancer |
last post by:
Hi,
to extend my skills, I am learning python. I have written small
program which computes math expression like "1+2*sin(y^10)/cos(x*y)"
and similar, so far only + - * / ^ sin con tan sqrt are supported. But
my program is quite inextensible, I have to change the code to add new
functions... Could some fellow experienced pythonista give me some
tips how to make my program shorter, and more extensible?
to use it, try something like...
|
by: Nagu |
last post by:
I am trying to save a dictionary of size 65000X50 to a local file and
I get the memory error problem.
How do I go about resolving this? Is there way to partition the pickle
object and combine later if this is a problem due to limited resources
(memory) on the machine (it is 32 bit machine Win XP, with 4GB RAM).
Here is the detail description of the error:
|
by: Nagu |
last post by:
I am trying to save a dictionary of size 65000X50 to a local file and
I get the memory error problem.
How do I go about resolving this? Is there way to partition the pickle
object and combine later if this is a problem due to limited resources
(memory) on the machine (it is 32 bit machine Win XP, with 4GB RAM).
Please advice.
Thank you,
|
by: Nagu |
last post by:
I didn't have the problem with dumping as a string. When I tried to
save this object to a file, memory error pops up.
I am sorry for the mention of size for a dictionary. What I meant by
65000X50 is that it has 65000 keys and each key has a list of 50
tuples.
I was able to save a dictionary object with 65000 keys and a list of
15-tuple values to a file. But I could not do the same when I have a
list of 25-tuple values for 65000 keys.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |