从python代码中删除非中断空间



我在尝试导入模块时出错:

from sklearn.model_selection import train_test_split 

SyntaxError:标识符中的无效字符

这是因为字符串末尾有一个不间断的空格:

x='from sklearn.model_selection import train_test_split '
x[-1:]

'\xa0'

我可以替换那个空间,然后像这样复制粘贴代码:

import unicodedata
new_str = unicodedata.normalize("NFKD", x)
print (new_str)

这个新字符串没有问题:

来自sklearn.model_selection导入train_testrongplit

但我想知道ipython笔记本是否有内置功能来纠正这些问题。

这个主题在jupyter笔记本的Github页面上讨论过。

链接 :

去除尾部空白(闭合(

编辑器中的尾随空白(打开(

编辑器中的尾随空白(打开(

iPython中没有这样的内置函数。使用本指南可以在iPython中创建自定义魔术命令

x='from sklearn.model_selection import train_test_split '
print(x.strip())

最新更新