我正在尝试在我的Jupyter笔记本中执行%load命令,但它没有按预期工作。我没有获取要加载的模块的内容,而是获得了一个包含模块信息的字典:
# %load test.ipynb
{
"cells": [
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2.3333333333333335\n"
]
}
],
"source": [
"def example(a, b, c):\n",
" return (a+b)/c\n",
"\n",
"x = 12\n",
"y = -5\n",
"z = 3\n",
"\n",
"result = example(x, y, z)\n",
"print(result)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
这是我期望得到的(这是来自'数据分析与Python'书籍的一个例子):
%load test.ipynb
def example(a, b, c):
return (a+b)/c
x = 12
y = -5
z = 3
result = example(x, y, z)
print(result)
有谁知道为什么这个例子不起作用? 提前谢谢!
答案 0 :(得分:0)
魔术函数%load
正在正确加载您指定的文件。您将看到json
文件的.ipnb
格式。
如果要将pythin模块加载到单元中以执行它,则必须删除包含有效python的.py
文件。
例如:
%load myawesomescript.py
您还可以加载.txt
,.csv
和其他格式。