python-3.x - 如何将python shell中的结果保存到变量中以便可以使用
问题描述
目前我正在尝试添加一个功能来在我的代码中搜索图像 url。我找到了下面的代码,但是当我运行它时,所有数据都显示(包括我想要的 url)在 python shell 中显示/打印,但没有保存到变量中。
我怎样才能得到它,以便将 shell 中显示的 url 保存到我可以使用的变量中。
谢谢
from google_images_download import google_images_download #importing the library
response = google_images_download.googleimagesdownload() #class instantiation
arguments = {"keywords":"air plane","limit":1,"print_urls":True} #creating list of arguments
paths = response.download(arguments) #passing the arguments to the function
解决方案
我不知道它是否会起作用,但你试过了吗
var = subprocess.check_output('python file.py',stderr=subprocess.STDOUT,shell=True)
这从 python 程序获取 shell 输出并存储它。虽然它认为它可能只是 print() 和 repr() 它将存储。
btw file.py 是生成所需输出的文件。
推荐阅读
- r - 如何删除R中当年没有观察到的所有观察值?
- python - 在训练和验证中丢弃最后一批(或添加一些条目)
- javascript - 节点异步/等待对我不起作用(使用 Postgres / 节点时 - 在进行下一次调用之前使用数据库更新)
- javascript - 单击按钮时如何创建过滤功能
- ios - 使用键盘时使文本文件向上滑动 swift 5
- python - 如何删除以短信形式发送的“[Attachment(s) removed]”字符串?
- svelte - 如何在预加载期间写入会话存储
- sql-server - 限制用户只能访问 ssis 包
- javascript - 提供来自 AuthProvider 的数据以存储在 React 中
- php - 如何使用 $_SERVER['QUERY_STRING'] 获取 url 中的部分查询字符串