str.split('')给出了一个字符串形式的句子"ValueError:empty separator"

Glu*_*ubb 3 python python-2.7

如果你想知道,我指定的分隔符是一个空格

words = stuff.split(" ")
Run Code Online (Sandbox Code Playgroud)

有问题的字符串是"这是一个问题的例子".我也尝试#作为分隔符并将#放入我的句子中,我得到了同样的错误.

编辑

好的,这是完整的块

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)
Run Code Online (Sandbox Code Playgroud)

当我将其作为py文件运行时,它可以工作.但是当我运行解释器,导入模块,并键入: str.split 后跟sentence = "This is an example of a sentence."

我得到了上面提到的错误.

是的,我意识到这是多余的,我只是在玩功能.

编辑:好的,这是整个追溯:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "ex25.py", line 6, in break_words
words = stuff.split(' ')
Run Code Online (Sandbox Code Playgroud)

编辑:嗯,我不知道我做了什么不同,但是当我现在再次尝试它时,它工作:

>>> s = "sdfd dfdf ffff"
>>> ex25.break_words(s)
['sdfd', 'dfdf', 'ffff']
>>> words = ex25.break_words(s)
>>>
Run Code Online (Sandbox Code Playgroud)

如你所见,没有错误.

小智 9

化作文字

text = 'Hello World'
print(text.split())

# ['Hello', 'World']
Run Code Online (Sandbox Code Playgroud)

变成字母

word = 'Hello'
print(list(word))

# ['H', 'e', 'l', 'l', 'o']
Run Code Online (Sandbox Code Playgroud)


小智 8

你从'Python the hardway'这个练习中遇到了同样的问题.我只需要在引号之间加一个空格.

def breakWords(stuff):
    """this function will break up words."""
    words = stuff.split(" ")
    return words
Run Code Online (Sandbox Code Playgroud)

也有人提到你必须重新加载模块.虽然在这个例子中,因为在windows中使用命令提示符我必须退出()然后重新启动我的py会话并再次导入练习.