user3046660
user3046660

Reputation: 81

ValueError: too may values to unpack

Could anyone give some input on this please.

for line in oscars_file:
    if '-' in line:
        years,oscars=line.strip().split('-')

I'm getting this error in the terminal:

ValueError: too many values to unpack (expected 2)

An example from the oscars file is:

1975 - "One Flew over the Cuckoo's Nest"

1973 - "The Sting"

Upvotes: 0

Views: 144

Answers (1)

sshashank124
sshashank124

Reputation: 32189

Some of your text may contain more than 1 '-'. For that, you should do:

for line in oscars_file:
    if '-' in line:
        years,oscars=line.strip().split('-',1)

The split('-',1) makes only one split which is the first split which is what you want.

Examples

>>> s = '1-2-3-4'
>>> print s.split('-',1)
['1','2-3-4']
>>> print s.split('-',2)
['1','2','3-4']

Upvotes: 4

Related Questions