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



我正在尝试系统地将给定的服务器conda依赖项存储在python字典中以进行内部版本控制。

在尝试转换在 Linux 操作系统上的 conda gpu 环境中运行的 python3.6 中的 conda 列表type(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等抓取任何 1 个单列。

如果您对前进的道路有任何想法,请提前感谢您!

你可以试试这个:

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))

这应该非常接近您想要的。

最新更新