gpt4 book ai didi

python - 如何修复Python中的ValueError : not enough values to unpack (expected 2, got 1)?

转载 作者:行者123 更新时间:2023-12-01 01:15:34 25 4
gpt4 key购买 nike

我想监控我的 docker 容器 ram 使用情况。现在我在网上找到了一个脚本,它生成一个 txt 文件,其统计信息如下:

LOG_FILE="/volume1/docker/docker-logs.txt"

while true;
do
sleep 10m
docker stats --format "{{.Name}}, {{.MemUsage}}" --no-stream >> $LOG_FILE
#echo "-" >> $LOG_FILE
done

让它在启动时与我的 Synology NAS 一起运行。工作正常,我得到这个文件:

-
Python3, 46.42MiB / 150MiB
hassio, 160.8MiB / 3.855GiB
Jacket, 255.4MiB / 3.855GiB
Radarrnodebug, 96.87MiB / 3.855GiB
Sonarrnodebug, 112.8MiB / 3.855GiB
Ombii, 212.2MiB / 3.855GiB
watchtower, 11.48MiB / 50MiB
-
Python3, 46.42MiB / 150MiB
hassio, 68.34MiB / 3.855GiB
Jacket, 258.3MiB / 3.855GiB
Radarrnodebug, 101.8MiB / 3.855GiB
Sonarrnodebug, 114.8MiB / 3.855GiB
Ombii, 212.4MiB / 3.855GiB
watchtower, 11.48MiB / 50MiB
-
Python3, 46.42MiB / 150MiB
hassio, 71.06MiB / 3.855GiB
Jacket, 262.7MiB / 3.855GiB
Radarrnodebug, 102.2MiB / 3.855GiB
Sonarrnodebug, 124.1MiB / 3.855GiB
Ombii, 217.7MiB / 3.855GiB
watchtower, 11.48MiB / 50MiB
-
Python3, 46.42MiB / 150MiB
hassio, 81.38MiB / 3.855GiB
Jacket, 262.7MiB / 3.855GiB
Radarrnodebug, 102.5MiB / 3.855GiB
Sonarrnodebug, 125.1MiB / 3.855GiB
Ombii, 217.6MiB / 3.855GiB
watchtower, 11.48MiB / 50MiB
-
Python3, 46.42MiB / 150MiB
hassio, 76.55MiB / 3.855GiB
Jacket, 269.2MiB / 3.855GiB
Radarrnodebug, 103.3MiB / 3.855GiB
Sonarrnodebug, 123.8MiB / 3.855GiB
Ombii, 219MiB / 3.855GiB
watchtower, 11.48MiB / 50MiB
-
Python3, 46.42MiB / 150MiB
hassio, 77.52MiB / 3.855GiB
Jacket, 268.4MiB / 3.855GiB
Radarrnodebug, 106.2MiB / 3.855GiB
Sonarrnodebug, 117.8MiB / 3.855GiB
Ombii, 213.1MiB / 3.855GiB
watchtower, 11.48MiB / 50MiB
-

现在为了把它变成一个漂亮的 PNG,我有以下内容:

from pip._internal import main
main(["install", "matplotlib", "numpy","pandas"])

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

log_path = "/stats/docker-logs.txt"

with open(log_path) as f:
raw_data = f.readlines()
nrows = 8
n = len(raw_data) // nrows
data = []
for i in range(n):
start = i * nrows
end = start + nrows - 1
d = raw_data[start:end]
datum = {}
datum['i'] = i
for line in d:
name, stats = line.strip().split(',')
stats = float(stats.split('/')[0].strip()[:-3])
datum[name] = stats
data.append(datum)

data = pd.DataFrame(data)
data['time (hour)'] = data['i'] * 10 / 60
ax = data.drop(columns='i').set_index('time (hour)').plot()
ax.set_ylabel('RAM Usage (MiB)')
ax.figure.savefig('/stats/plot.png')

一开始工作正常,但突然停止工作,我收到以下错误:

Traceback (most recent call last):
File "/stats/genstat.py", line 22, in <module>
name, stats = line.strip().split(',')
ValueError: not enough values to unpack (expected 2, got 1)

做了一些调试,发现

var i

变成了 0,所以没有任何作用了(因为它必须跳过“-”字符)发生了什么以及如何修复它?我已经尝试了很多,删除了“-”字符,改变了行,但没有什么真正重要的(对meeeee)

希望有帮助

最佳答案

这一行

name, stats = line.strip().split(',')

失败,因为输入文件中有一行没有逗号。最后很可能是一个空行。做

columns = line.strip().split(',')
if len(columns) == 2:
name, stats = columns
else:
print("Expected name and stats, got", columns)

关于python - 如何修复Python中的ValueError : not enough values to unpack (expected 2, got 1)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54388038/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com