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.

Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
theprowler
  • 3,138
  • 11
  • 28
  • 39

5 Answers5

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)
kindall
  • 178,883
  • 35
  • 278
  • 309
  • 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` – theprowler Mar 24 '17 at 17:53
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)
Ryan
  • 2,869
  • 2
  • 12
  • 20
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'
Aaron
  • 10,133
  • 1
  • 24
  • 40
  • 1
    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 Mar 24 '17 at 18:03
  • @theprowler I missed the spaces for 'or' in the if statement as well... see edit – Aaron Mar 24 '17 at 18:05
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 ')
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
  • 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)` – theprowler Mar 24 '17 at 17:49
  • 1
    If you got too many things to unpack, it means that there was more than one `'to '` and `'or '`. – Stephen Rauch Mar 24 '17 at 17:52
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

Neill Herbst
  • 2,072
  • 1
  • 13
  • 23