micaldas
micaldas

Reputation: 11

Python3 - "ValueError: not enough values to unpack (expected 3, got 1)"

I'm very new to Python and programming overall, so if I seem to struggle to understand you, please bear with me. I'm reading "Learn Python 3 the Hard Way", and I'm having trouble with exercise 23.

I copied the code to my text editor and ended up with this:

import sys

script, input_encoding, error = sys.argv


def main(language_file, encoding, errors):
    line = language_file.readline()

    if line:
        print_line(line, encoding, errors)
        return main(language_file, encoding, errors)


def print_line(line, encoding, errors):
    next_lang = line.strip()
    raw_bytes = next_lang.encode(encoding, errors=errors)
    cooked_string = raw_bytes.decode(encoding, errors=errors)

    print(raw_bytes, "<====>", cooked_string)


languages = open("languages.txt", encoding = "utf-8")

main(languages, input_encoding, error)

When I tried to run it I got the following error message:

Traceback (most recent call last):
  File "pag78.py", line 3, in <module>
    script, input_encoding, error = sys.argv
ValueError: not enough values to unpack (expected 3, got 1)

which I am having difficulties understanding in this context.

I googled the exercise, to compare it something other than the book page and, if I'm not missing something, I copied it correctly. For example, see this code here for the same exercise.

Obviously something is wrong with this code, and I'm not capable to identify what it is. Any help would be greatly appreciated.

Upvotes: 1

Views: 2610

Answers (3)

I started reading LPTHW a couple of weeks ago. I got the same error as 'micaldras'. The error arises because you have probably clicked the file-link and opened an IEExplorer window. From there, (I guess), you have copied the text into a notepad file and saved. it. I did that as well and got the same errors. I then downloaded the file directly from the indicated link (right click on the file and choose Save Target As). The saves the file literally as Zed intended and the program now runs.

Upvotes: 0

Harvey M
Harvey M

Reputation: 51

When you run the program, you have to enter your arguments into the command line. So run the program like this:

python ex23.py utf-8 strict

Copy and paste all of that into your terminal to run the code. This exercise uses argv like others do. It says this in the chapter, just a little bit later. I think you jumped the gun on running the code before you got to the explanation.

Upvotes: 2

Ondrej K.
Ondrej K.

Reputation: 9664

Let's record this in an answer for sake of posterity. In short, the immediate problem described lies not as much in the script itself, but rather in how it's being called. No positional argument was given, but two were expected to be assigned to input_encoding and error.

This line:

script, input_encoding, error = sys.argv

Takes (list) of arguments passed to the script. (sys.argv) and unpacks it, that is asigns its items' values to the variables on the left. This assumes number of variables to unpack to corresponds to items count in the list on the right.

sys.argv contains name of the script called and additional arguments passed to it one item each.

This construct is actually very simple way to ensure correct number of expected arguments is provided, even though as such the resulting error is perhaps not the most obvious.

Later on, you certainly should check out argparse for handling of passed arguments. It is comfortable and quite powerful.

Upvotes: 0

Related Questions