'not enough values to unpack (expected 2, got 1) in python
Code:
import re
abc=("Above them, her thick black brows slanted upward, cutting a " , "startling oblique line above her hows thick iwa her magnolia-white skin--that skin so" ,"rews ghij the Cutting ABove"
for line in abc:
line_string = line.strip()
processed_string = re.sub(r'[^a-zA-Z0-9\ ]', '', line_string).lower()
word, count = processed_string.split('\t', 1)
I am getting the error below:
File "C:\\Users\\Simar\\untitled4.py", line 17, in \<module\>
word, count = processed_string.split('\\t', 1)
ValueError: not enough values to unpack (expected 2, got 1)
How can this be fixed?
Solution 1:[1]
So, you're getting this error because the last line of your code is trying to split strings based on tab characters, but there aren't any tab characters in your input.
If you print the value generated by the last line of the loop, rather than assigning it, you see that the right-hand side of the assignment contains a single string, whereas the word, count = code expects a tuple that will unpack to two values.
import re
abc=("Above them, her thick black brows slanted upward, cutting a " , "startling oblique line above her hows thick iwa her magnolia-white skin--that skin so" ,"rews ghij the Cutting ABove"
for line in abc:
line_string = line.strip()
processed_string = re.sub(r'[^a-zA-Z0-9\ ]', '', line_string).lower()
print(processed_string.split('\t', 1))
This gives, as output:
['above them her thick black brows slanted upward cutting a']
['startling oblique line above her hows thick iwa her magnoliawhite skinthat skin so']
['rews ghij the cutting above']
It's unclear where you want the value of count to come from, so I can't suggest a fix for that without a bit more information.
Solution 2:[2]
The lines you are trying to process do not have the character \t.
Therefore, even if you put the maxsplit to 1, you will have only one value (not two) because splitting doesn't have any effect.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | baileythegreen |
| Solution 2 | Eli Harold |
