[英]Getting the output of a shell command in Jupyter Notebook
如果我有一个带有以下脚本的 python 文件:
import pandas as pd
def csv():
r = pd.DataFrame([[1,2,3,6],[1,3,4,5]])
return r
csv()
How can I use a shell command in Jupyter Notebook to get the output of this script which is a pandas DataFrame. Or How do I modify the python script to do so but sill use a shell command in Jupyter (For some reason I need a shell command rather than just importing the function from the python file and executing it)
到目前为止我尝试的是以下但没有工作(希望 Pandas DataFrame 作为输出)
import subprocess
result = subprocess.run(['python','MyPythonFile.py'], stdout=subprocess.PIPE)
或者
result = ! python MyPythonFile.py
访问结果的“stdout”参数:
import subprocess
result = subprocess.run(['python','MyPythonFile.py'], stdout=subprocess.PIPE)
print(result.stdout)
编辑:您不能通过 unix 标准输出传递 DataFrame。 DataFrame 是 Python object,对操作系统没有意义。 使用导入在另一个 python 脚本中执行您的代码,或通过 pickle 或类似工具导出生成的对象。 如果你不完全改变你的策略,你试图完成的事情是不可能的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.