Python str.split(' ') 给我一个字符串形式的句子的“ValueError: empty separator”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20826788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
str.split(' ') giving me "ValueError: empty separator" for a sentence in the form of a string
提问by Glubbdrubb
TLDR:
If you don't specify a character for str.splitto 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.
TLDR:如果您没有指定str.split要拆分的字符,则默认为空格或制表符。我的错误是由于我的引号之间没有空格。
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.
有问题的字符串是This is an example of a question.我也尝试#作为分隔符并将#'s 放入我的句子中并得到相同的错误。
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)
当我将它作为 py 文件运行时,它可以工作。但是当我运行解释器,导入模块,并键入:
sentence = "This is an example of a sentence."后跟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:
编辑 2:这是整个追溯:
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:
编辑 3:嗯,我不知道我做了什么不同的事情,但是当我现在再次尝试时,它起作用了:
>>> s = "sdfd dfdf ffff"
>>> ex25.break_words(s)
['sdfd', 'dfdf', 'ffff']
>>> words = ex25.break_words(s)
>>>
As you can see, no errors.
如您所见,没有错误。
采纳答案by Matthew
ya had same issue on this exercise from 'Python the hardway'. I just had to put a space between the quote marks.
你在“Python the hardway”的这个练习中遇到了同样的问题。我只需要在引号之间加一个空格。
def breakWords(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 using a command prompt in windows i had to exit() then restart my py session and import the exercise again.
也正如有人提到的那样,您必须重新加载模块。虽然在这个例子中,因为在 Windows 中使用命令提示符,我不得不 exit() 然后重新启动我的 py 会话并再次导入练习。
回答by Vorsprung
As the debugger output below shows, this error is generated by an empty parameter to 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.
您的代码必须传递一个空值才能拆分。解决这个问题,错误就会消失。
回答by Tianqi Tong
回答by cansdmz
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
我遇到了完全相同的问题。最初的错误是由于空分隔符 '',我忘了在里面放一个空格。修改完代码后,需要退出Python,然后重启Python并导入ex25。它会工作。如果您没有退出 Python 并再次导入代码,它将无法工作。或者最简单的方法是重新加载(ex25),然后它就可以解决问题。希望能有所帮助
回答by Alex
I had the same problem, when learning from book - Learn the Python the hard way,
我在从书本上学习时遇到了同样的问题 - 以艰难的方式学习 Python,
removing 2 apostrophe("") solved the issue for me.
删除 2 个撇号("") 为我解决了这个问题。
words = stuff.split() # removing apostrophes eliminates the error
words = stuff.split() # 去掉撇号消除错误
回答by Kathiravan Natarajan
I got an issue similar to the same Error.
我遇到了与相同错误类似的问题。
But the thing is, I missed space in split(''") function. # Value Error : Empty Separator
但问题是,我错过了 split(''") 函数中的空间。# Value Error : Empty Separator
If you insert a space between the apostrophes, error is fixed
如果在撇号之间插入一个空格,错误是固定的
回答by diamind
You definitely don't need quotes inside the parenthesis.
您绝对不需要括号内的引号。
sentence = "bla mla gla dla"
sentence.split()
will give you
会给你
['bla', 'mla', 'gla', 'dla']
['bla', 'mla', 'gla', 'dla']
as a result by default.
结果默认。


