ValueError: need more than 1 value to unpack

A forum for general discussion of the Python programming language.

ValueError: need more than 1 value to unpack

Postby forum_dweller » Fri Jul 11, 2014 10:35 pm

My program is supposed to decrypt a previously encrypted file. But, I keep receiving the error mentioned in the subject. Here is the code:

Code: Select all
encryptionDirectory = {"`": "~", "1": "!", "2": "@", "3": "#", \
"4": "$", "5": "%", "6": "^", "7": "&", "8": "*", "9": ")", "0": "(", \
"-": "_", "=": "+", "q": "M", "w": "N", "e": "B", "r": "V", "t": "C", \
"y": "X", "u": "Z", "i": "L", "o": "K", "p": "J", "]": "}", "[": "{", "\\": "|",  \
"a": "H", "s": "G", "d": "F", "f": "D", "g": "S", "h": "A", "j": "P", \
"k": "O", "l": "I", ";": ":", "'": "\"", "z": "U", "x": "Y", "c": "T", \
"v": "R", "b": "E", "n": "W", "m": "Q", ",": "<", ".": ">", "/": "?"}
         fileObject1 = open("Chapter10_Q3_File-Encrypted.txt", "r")
         fileObject2 = open("Chapter10_Q3_File-Decrypted.txt", "w")
         string = fileObject1.readlines()
         for key, value in encryptionDirectory:
            if value in string:
               fileObject2.write(key)
         fileObject1.close()
         fileObject2.close()


The error is being received on the "for" loop line. Any thoughts?
forum_dweller
 
Posts: 9
Joined: Sun Jun 29, 2014 7:18 pm

Re: ValueError: need more than 1 value to unpack

Postby micseydel » Fri Jul 11, 2014 11:29 pm

Full backtrace please, not just the error type.

Further, you should reproduce your problem using a short code snippet that doesn't require external files. Just have variables store small strings that reproduce it. Also make sure the indentation is not incorrect, etc.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1291
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: ValueError: need more than 1 value to unpack

Postby Yoriz » Fri Jul 11, 2014 11:40 pm

Adding iteritems() to that line should cure the error.
Code: Select all
for key, value in encryptionDirectory.iteritems():
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 789
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: ValueError: need more than 1 value to unpack

Postby forum_dweller » Sat Jul 12, 2014 12:42 am

Changing the for line to to the following:

for key, value in encryptionDirectory.iteritems():

Results in the following:

AttributeError: 'dict' object has no attribute 'iteritems'
forum_dweller
 
Posts: 9
Joined: Sun Jun 29, 2014 7:18 pm

Re: ValueError: need more than 1 value to unpack

Postby forum_dweller » Sat Jul 12, 2014 12:44 am

Here is my file.
forum_dweller
 
Posts: 9
Joined: Sun Jun 29, 2014 7:18 pm

Re: ValueError: need more than 1 value to unpack

Postby micseydel » Sat Jul 12, 2014 12:50 am

If you're using Python 3, just use items() without "iter". Python 3 now defaults to a lot of things being iterators, so doesn't have explicit methods for them as Python 2 did.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1291
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: ValueError: need more than 1 value to unpack

Postby forum_dweller » Sat Jul 12, 2014 1:01 am

micseydel wrote:If you're using Python 3, just use items() without "iter". Python 3 now defaults to a lot of things being iterators, so doesn't have explicit methods for them as Python 2 did.


I am trying to figure out, though, how to determine if the index of the file object (each character contained within), is present in encryption directory (as a value), then write the corresponding key to the second file.

I can do the cheap way (longer code way) of simply reversing the keys/values in a new dictionary, or making a list of the keys, then the values, and then nest them in a for loop with one index to do the task, but I want to figure it out this way.
forum_dweller
 
Posts: 9
Joined: Sun Jun 29, 2014 7:18 pm

Re: ValueError: need more than 1 value to unpack

Postby forum_dweller » Sat Jul 12, 2014 1:04 am

The items() returns a dict of tuples. I dont know what to do with that inside another list?

([( )])

??
forum_dweller
 
Posts: 9
Joined: Sun Jun 29, 2014 7:18 pm

Re: ValueError: need more than 1 value to unpack

Postby micseydel » Sat Jul 12, 2014 9:58 am

Just create a second map that does the reverse mapping. Using a loop will be very inefficient, probably making that part of your algorithm O(n**2) instead of O(n).

items() does not return a dictionary. It returns a set-like collection of two-tuples containing key and value. Take your original map, and to create the new reversed map make the keys in the new map the second part of each tuple, and the values the corresponding first part.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1291
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 2 guests