首页 > 解决方案 > 从 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_test_split

但我想知道 ipython notebook 是否具有纠正此类问题的内置功能。

标签: pythonjupyter-notebook

解决方案


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


推荐阅读