【问题标题】:remove non-breaking space from python code从 python 代码中删除不间断空格
【发布时间】:2019-11-27 16:58:12
【问题描述】:

尝试导入模块时出现错误:

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

【问题讨论】:

    标签: python jupyter-notebook


    【解决方案1】:

    这个主题在 jupyter-notebook 的 Github 页面上讨论过。

    链接

    Strip trailing whitespace(已关闭)

    Trailing whitespace in editor(打开)

    Trailing whitespace in editor(打开)

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 有什么方法可以编写一个触发器,在执行单元格时运行一个函数?我不想按照建议在每个单元格的开头添加@cell_magic。魔法应该在飞行中发生:)
      • 你可以这样写一个jquery:stackoverflow.com/a/38856870/6537262
      • 该代码将在笔记本加载时自动运行所有单元格。我正在尝试在单个单元格执行时自动运行。
      【解决方案3】:
      x='from sklearn.model_selection import train_test_split '
      print(x.strip())
      

      【讨论】:

        猜你喜欢
        • 2011-02-05
        • 2014-01-25
        • 2012-08-10
        • 2012-07-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多