系统地将conda列表的输出存储到python字典中

2024-05-23 14:58:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在python字典中系统地存储给定的服务器和依赖项,以便进行内部版本控制

在Linux操作系统上的conda gpu环境下运行的python3.6中,我无法尝试转换conda listtype(string)的输出

conda list >

# Name                    Version                   Build  Channel
_libgcc_mutex             0.1                        main
_tflow_select             2.1.0                       gpu
absl-py                   0.4.1                    py35_0
aiohttp                   3.4.2            py35h7b6447c_0
apipkg                    1.5                      py35_0
astor                     0.7.1                    py35_0
async-timeout             3.0.0                    py35_0
attrs                     19.3.0                     py_0
blas                      1.0                         mkl
ca-certificates           2019.11.28           hecc5488_0    conda-forge

然后从我尝试过的python脚本中:

conda_list_output = os.popen('conda list').read()
conda_list_output = {conda_list_output}
conda_list_output = eval(conda_list_output)
conda_list_output =pd.DataFrame([conda_list_command], columns['asdf']) #Thought since 4 columns, might be easier to convert to a df right away.

我对os.popen的工作比较陌生,我不知道这是否是最好的前进道路。此外,我不知道如何通过任何linux过滤器获取任何一列,grepcut,等等

如果你对未来有任何想法,请提前感谢


Tags: columnstopy服务器output字典gpuos
1条回答
网友
1楼 · 发布于 2024-05-23 14:58:57

你可以试试这个:

import conda.cli.python_api as Conda
import re

output = Conda.run_command(Conda.Commands.LIST)
# output is a tuple, also containing the exitcode
data = output[0].split("\n")

# define extraction method:
def get_words(line): return re.findall("[^\s]+",line)

# skip header row, andfinal line termination
words = [get_words(x) for x in data[3:-1]
keys, vals = [w[0] for w in words], [w[1] for w in words]
result = dict(zip(keys,vals))

这应该非常接近你想要的

相关问题 更多 >