theprowler
theprowler

Reputation: 3600

Python - split() producing ValueError

I am trying to split the line:

American plaice - 11,000 lbs @ 35 cents or trade for SNE stocks

at the word or but I receive ValueError: not enough values to unpack (expected 2, got 1).

Which doesn't make sense, if I split the sentence at or then that will indeed leave 2 sides, not 1.

Here's my code:

if ('-' in line) and ('lbs' in line):
    fish, remainder = line.split('-') 
    if 'trade' in remainder:
        weight, price = remainder.split('to ')
        weight, price = remainder.split('or')

The 'to' line is what I normally use, and it has worked fine, but this new line appeared without a 'to' but instead an 'or' so I tried writing one line that would tackle either condition but couldn't figure it out so I simply wrote a second and am now running into the error listed above.

Any help is appreciated, thanks.

Upvotes: 0

Views: 725

Answers (5)

Aaron
Aaron

Reputation: 11075

You split on 'to ' before you attempt to split on 'or', which is throwing the error. The return value of remainder.split('to ') is [' 11,000 lbs @ 35 cents or trade for SNE stocks'] which cannot be unpacked to two separate values. you can fix this by testing for which word you need to split on first.

if ('-' in line) and ('lbs' in line):
    fish, remainder = line.split('-') 
    if 'trade' in remainder:
        if 'to ' in remainder:
            weight, price = remainder.split('to ')
        elif ' or ' in remainder:
            weight, price = remainder.split(' or ') #add spaces so we don't match 'for'

Upvotes: 1

Ryan
Ryan

Reputation: 3149

This should solve your problem by checking if your separator is in the string first.

Also note that split(str, 1) makes sure that your list will be split a max of one time (Ex "hello all world".split(" ", 1) == ["hello", "all world"])

if ('-' in line) and ('lbs' in line):
    fish, remainder = line.split('-') 
    if 'trade' in remainder:
        weight, price = remainder.split(' to ', 1) if ' to ' in remainder else remainder.split(' or ', 1)

Upvotes: 1

Stephen Rauch
Stephen Rauch

Reputation: 49784

Once you have done the split(), you have a list, not a string. So you can not do another split(). And if you just copy the line, then you will overwrite you other results. You can instead try and do the processing as a string:

weight, price = remainder.replace('or ', 'to ').split('to ')

Upvotes: 0

kindall
kindall

Reputation: 184101

The most straightforward way is probably to use a regular expression to do the split. Then you can split on either word, whichever appears. The ?: inside the parentheses makes the group non-capturing so that the matched word doesn't appear in the output.

import re
# ...
weight, price = re.split(" (?:or|to) ", remainder, maxsplit=1)

Upvotes: 2

Neill Herbst
Neill Herbst

Reputation: 2122

The problem is that the word "for" also contains an "or" therefore you will end up with the following:

a = 'American plaice - 11,000 lbs @ 35 cents or trade for SNE stocks'
a.split('or')

gives

['American plaice - 11,000 lbs @ 35 cents ', ' trade f', ' SNE stocks']

Stephen Rauch's answer does fix the problem

Upvotes: 0

Related Questions