#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2014
    Posts
    14
    Rep Power
    0

    Question ValueError: need more than 1 value to unpack


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

    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?
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    620
    Rep Power
    65
    Hint ...
    Code:
    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", ",": "<", ".": ">", "/": "?"}
    
    for key, value in encryptionDirectory.items():
        print(key, value)  # test
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    530
    Rep Power
    34
    And this statement will never be true because readlines returns a list of strings
    if value in string:
    The sub-value of the list is the entire string, so you are comparing a single character to the complete string. You have to access each string individually, for which the sub-value is a character as illustrated below
    Code:
    ## name this program "test_1.py and use it as an input test
    readlines_test=open("./test_1.py", "r").readlines()
    if "readlines" in readlines_test:
        print "Found"
    else:
        print "Not Found"
    
    ##------------ Use a for() loop instead
    for record in readlines_test:
        if "readlines" in record:
            print record
    Last edited by dwblas; July 12th, 2014 at 01:26 PM.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2014
    Posts
    14
    Rep Power
    0

    Methods, Methods


    Originally Posted by dwblas
    And this statement will never be true because readlines returns a list of strings
    if value in string:
    The sub-value of the list is the entire string, so you are comparing a single character to the complete string. You have to access each string individually, for which the sub-value is a character as illustrated below
    Code:
    ## name this program "test_1.py and use it as an input test
    readlines_test=open("./test_1.py", "r").readlines()
    if "readlines" in readlines_test:
        print "Found"
    else:
        print "Not Found"
    
    ##------------ Use a for() loop instead
    for record in readlines_test:
        if "readlines" in record:
            print record
    I reversed the ordering of the keys/values, and changed method to get(). What I do not understand with Python is why they do not implement a easy/direct method to get a key from a specified value without the extra code, yet they give me things like a read, readline, and readlines method(s)...???
  8. #5
  9. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    620
    Rep Power
    65
    Dictionary keys are unique (they are the index), while values are not. So for a given value there can be several keys.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

IMN logo majestic logo threadwatch logo seochat tools logo