Str. Split(' ') Giving Me "Valueerror: Empty Separator" for a Sentence in the Form of a String
Tldr: If You Don't Specify a Character for Str. Split to Split By, It Defaults to a Space or Tab Character. My Error Was Due to the Fact That I Did Not Have a...
TLDR:
If you don't specify a character for str.split to split by, it defaults to a space or tab character. My error was due to the fact that I did not have a space between my quotes.
In case you were wondering, the separator I specified is a space:
words = stuff.split(" ")
The string in question is This is an example of a question.
I also tried # as the separator and put #'s into my sentence and got the same error.
Edit: Here is the complete block
def break_words(stuff):
"""This function will break up words for us."""
words = stuff.split(" ")
return words
sentence = "This is an example of a sentence."
print break_words(sentence)
When I run this as py file, it works.
but when I run the interpreter, import the module, and type:
sentence = "This is an example of a sentence."
followed by print break_words(sentence)
I get the above mentioned error.
And yes, I realise that this is redundant, I'm just playing with functions.
Edit 2: Here is the entire traceback:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "ex25.py", line 6, in break_words
words = stuff.split(' ')
Edit 3: Well, I don't know what I did differently, but when I tried it again now, it worked:
>>> s = "sdfd dfdf ffff"
>>> ex25.break_words(s)
['sdfd', 'dfdf', 'ffff']
>>> words = ex25.break_words(s)
>>>
As you can see, no errors.
10 Answers
I had the same issue on this exercise from Learn Python the Hard Way. I just had to put a space between the quote marks.
def break_words(stuff):
"""this function will break up words."""
words = stuff.split(" ")
return words
also, as someone mentioned, you have to reload the module. Although in this example, since I was using a command prompt on Windows, I had to exit() then restart my py session and import the exercise again.
Turn into words
text = 'Hello World'
print(text.split())
# ['Hello', 'World']
Turn into letters
word = 'Hello'
print(list(word))
# ['H', 'e', 'l', 'l', 'o']
As the REPL output below shows, this error is generated by passing an empty string to str.split()
>>> s = "abc def ghi jkl"
>>> s.split(" ")
['abc', 'def', 'ghi', 'jkl']
>>> s.split("")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: empty separator
>>>
Your code must be passing an empty value to split. Fix this and the error will go away.
You definitely don't need quotes inside the parenthesis.
sentence = "bla mla gla dla"
sentence.split()
will give you
['bla', 'mla', 'gla', 'dla']
as a result by default.
I got an issue similar to the same Error.
But the thing is, I missed space in split(''") function. # Value Error : Empty Separator
If you insert a space between the apostrophes, error is fixed
Add a space to there:
words = stuff.split(" ")Reload your interpreter
I had the exact same problem.The original error was due to the empty seperator '', which I forgot to put a blank in it. After you modified the code, you need to exit Python, and then restart Python and import the ex25. It would work. If you did not exit Python and just import the code again, it would not work. Or the simplest way is to reload(ex25), then it would solve the problem. Hope that may help
I had the same problem, when learning from book - Learn the Python the hard way,
removing 2 apostrophe("") solved the issue for me.
words = stuff.split() # removing apostrophes eliminates the error
If you want to split a word into characters, use the list() method instead:
word = "Easy"
x = list(word)
print(x)
#["w", "o", "r", "d"]
Running Windows 10 from the powershell. ***Python the Hardway ***
I removed the ('') from the split() method. Also, read the study drill #3. It mentions used "from ex25 import *". Only these two changes allowed me to run from the interpreter without a traceback. After changing the import line, it runs as written in the book.
Prior to, in the interpreter (not ex25.py) I had to change line 3 'words = break_words(sentence)' to 'functWords = break_words(sentence)'.