jupyter-notebook - shell command # can't be carry out when it not used for comments on colab
问题描述
I'm confused about this code! Why # cant't play a role that takes the length of a string?
string="abcd"
!echo ${#string}
In fact, the code behind # has become commented and cannot be executed!
Any advice?
解决方案
这可以正常工作,但是您不能以这种方式混合 python 和 bash 变量。试试这个:
!string="abcd" && echo ${#string}
这两个语句必须在同一行,因为在 IPython 中,每个!
语句都会打开一个临时子 shell,并且变量不会在 shell 之间持久化。如果你想使用多行 bash 程序,你可以使用%%bash
cell magic 来代替:
%%bash
string="abcd"
echo ${#string}
推荐阅读
- java - 如何将 Spring @Configuration 类限制为仅实例化备用服务?
- c++ - 如何组合异步操作?
- jquery - 选择页面锚链接时如何关闭移动菜单
- python - 使用线程模块时,Django在完成测试后不退出
- sql - Oracle SQL 中缺少选择关键字
- git - Git如何使用远程仓库的`set-url`
- netlogo - 如何在 netlogo 中绘制分布?
- python - 基于图像掩码的 OpenCV 叠加 2 图像
- angular - Reference.push 失败:第一个参数在属性“Questions.code”中包含未定义
- python - TypeError:get() 为参数“task_id”获取了多个值