从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笔记本是否具有内置功能来纠正此类问题。

fishtreerain 回答:从python代码中删除不间断空间

iPython中没有这样的内置函数。可以使用guide

在iPython中创建自定义魔术命令 ,

该主题在jupyter-notebook的Github页面上讨论。

链接

Strip trailing whitespace(关闭)

Trailing whitespace in editor(打开)

Trailing whitespace in editor(打开)

,
x='from sklearn.model_selection import train_test_split '
print(x.strip())
本文链接:https://www.f2er.com/3153672.html

大家都在问