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

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 Byte
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 11138
dev7060
636 Expert 512MB
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(infile) 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
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...
1
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...
26
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...
4
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...
3
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...
14
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
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...
2
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...
0
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...
1
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...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.