如何使用Python逐个拆分动态数组值?

2024-09-28 23:16:30 发布

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

我们正在打印时获取数组值。获取以下输出。命令:

import subprocess
output = [subprocess.check_output("kapacitor list tasks | grep -i enabled | cut -d ' ' -f1", shell=True)]

打印阵列时:

`['tAlert_ALL_metrics_cpu\ntAlert_ALL_metrics_memory_usage\ntAlert_ALL_oracle_TBS_offline\ntAlert_NN_WMS_endpoint-message-count_MSE\ntAlert_NN_ecom_version_check\ntAlert_NN_ecom_version_check_all_farms\ntAlert_NN_metrics_fileSystem\ntAlert_NWNA_metrics_fileSystem\n'] `

预期输出如下:

output[0]= tAlert_ALL_metrics_cpu\ntAlert_ALL_metrics_memory_usage
output[1]= tAlert_ALL_oracle_TBS_offline
output[2]= tAlert_NN_WMS_endpoint-message-count_MSE

同时在检查数组长度时,显示为1

length = len(output)

Tags: outputcheckusagenn数组cpuallmetrics
1条回答
网友
1楼 · 发布于 2024-09-28 23:16:30

假设你的输出是

['tAlert_ALL_metrics_cpu\ntAlert_ALL_metrics_memory_usage\ntAlert_ALL_oracle_TBS_offline\ntAlert_NN_WMS_endpoint-message-count_MSE\ntAlert_NN_ecom_version_check\ntAlert_NN_ecom_version_check_all_farms\ntAlert_NN_metrics_fileSystem\ntAlert_NWNA_metrics_fileSystem\n']

然后你可以这样做:

output = ['tAlert_ALL_metrics_cpu\ntAlert_ALL_metrics_memory_usage\ntAlert_ALL_oracle_TBS_offline\ntAlert_NN_WMS_endpoint-message-count_MSE\ntAlert_NN_ecom_version_check\ntAlert_NN_ecom_version_check_all_farms\ntAlert_NN_metrics_fileSystem\ntAlert_NWNA_metrics_fileSystem\n']
outputlist = ''.join(output).split("\n")
for x in outputlist:
  print(x)

相关问题 更多 >