473,608 Members | 2,592 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

python, when I load a dictionary using pickle, only the first key and value print when I try to print all contents of the dictionary

10 New Member
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.

Expand|Select|Wrap|Line Numbers
  1. import pickle
  2.  
  3.  
  4. print("""\n 
  5.       1- View  codes
  6.       2- Add code
  7.       3- Create file/add first""")
  8.  
  9. while True:
  10.     choice = input("What is your choice?")
  11.  
  12.     DC_Message = {}
  13.  
  14.  
  15.     if choice == "1":
  16.         filename = "dcoutput"
  17.         infile = open(filename, 'rb')
  18.         DC_Message2 = pickle.load(infile)
  19.         infile.close()
  20.         for key, value in DC_Message2.items():
  21.             print(key, ' : ', value)
  22.  
  23.  
  24.     elif choice == "2":
  25.         DCcode = str(input("What is the new DCcode?"))
  26.         Message = str(input("What is the new message"))
  27.         DC_Message[DCcode] = Message
  28.         print(DC_Message)
  29.         for key, value in DC_Message.items():
  30.             outfile = open("dcoutput", 'ab')
  31.             pickle.dump(DC_Message, outfile)
  32.             outfile.close()
  33.             print(key, ' : ', value)
  34.  
  35.     elif choice == "3":
  36.         DCcode = str(input("What is the new DCcode?"))
  37.         Message = str(input("What is the new message"))
  38.         DC_Message[DCcode] = Message
  39.         print(DC_Message)
  40.         for key, value in DC_Message.items():
  41.             outfile = open("dcoutput", 'wb')
  42.             pickle.dump(DC_Message, outfile)
  43.             outfile.close()
  44.             print(key, ' : ', value)
Sep 21 '22 #1
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.

Expand|Select|Wrap|Line Numbers
  1. import pickle
  2.  
  3.  
  4. print("""\n 
  5.       1- View  codes
  6.       2- Add code
  7.       3- Create file/add first""")
  8.  
  9. while True:
  10.     choice = input("What is your choice?")
  11.  
  12.     DC_Message = {}
  13.  
  14.  
  15.     if choice == "1":
  16.         filename = "dcoutput"
  17.         infile = open(filename, 'rb')
  18.         DC_Message2 = pickle.load(infile)
  19.         infile.close()
  20.         for key, value in DC_Message2.items():
  21.             print(key, ' : ', value)
  22.  
  23.  
  24.     elif choice == "2":
  25.         DCcode = str(input("What is the new DCcode?"))
  26.         Message = str(input("What is the new message"))
  27.         DC_Message[DCcode] = Message
  28.         print(DC_Message)
  29.         for key, value in DC_Message.items():
  30.             outfile = open("dcoutput", 'ab')
  31.             pickle.dump(DC_Message, outfile)
  32.             outfile.close()
  33.             print(key, ' : ', value)
  34.  
  35.     elif choice == "3":
  36.         DCcode = str(input("What is the new DCcode?"))
  37.         Message = str(input("What is the new message"))
  38.         DC_Message[DCcode] = Message
  39.         print(DC_Message)
  40.         for key, value in DC_Message.items():
  41.             outfile = open("dcoutput", 'wb')
  42.             pickle.dump(DC_Message, outfile)
  43.             outfile.close()
  44.             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.

Expand|Select|Wrap|Line Numbers
  1. while True:
  2.     try:
  3.         print(pickle.load(infile))
  4.     except EOFError:
  5.         break
Oct 4 '22 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
3578
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...
1
6343
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)
26
4042
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"%>
4
2381
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.
3
1625
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...
14
28284
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]
4
1390
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...
2
4500
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:
0
1734
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,
1
6258
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.
0
8057
marktang
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...
0
7998
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,...
0
8470
jinu1996
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...
1
8142
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,...
0
6813
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4022
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2472
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
1580
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1327
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.