0

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.

4
  • 1
    There's isn't a 'to ' in that line Commented Mar 24, 2017 at 17:45
  • I know, that's why I added the line below it with 'or'....which is the line it fails on, even when I comment out the 'to' line Commented Mar 24, 2017 at 17:46
  • 1
    I actually got too many items to unpack.. the remainder splits on 'or' and 'for' Commented Mar 24, 2017 at 17:47
  • 2
    your error is that when trying to split first on 'to ', it returns a list of length 1, which cannot be unpacked into weight and price Commented Mar 24, 2017 at 17:52

5 Answers 5

2

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Ohhh gotcha I didn't think it would be that simple using RegEx to do this. Running that line of code though produced a syntax error saying keyword can't be an expression
1

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'

2 Comments

Ok I gotcha. I didn't think 'or' would also capture 'for' but it makes sense that it does. Thanks for the help and the explanation, it was really clear
@theprowler I missed the spaces for 'or' in the if statement as well... see edit
1

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)

Comments

0

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

Comments

0

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 ')

2 Comments

Are you sure? Because in the rest of my code (which I didn't add above) I do in fact do several splits after each other without an error.... I am a novice and I'm not telling you you're wrong but I can assure you my code works using split() several times in a row. Also, I tried your line of code and it failed :( it produced ValueError: too many values to unpack (expected 2)
If you got too many things to unpack, it means that there was more than one 'to ' and 'or '.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.