首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何修复ValueError:在python中没有足够的值来解包(预期为2,获取为1)?

如何修复ValueError:在python中没有足够的值来解包(预期为2,获取为1)?
EN

Stack Overflow用户
提问于 2019-01-27 20:18:31
回答 3查看 8K关注 0票数 2

我想监控我的码头容器内存的使用情况。现在我在网上找到了一个脚本,它生成一个包含如下统计数据的txt文件:

代码语言:javascript
复制
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启动时运行。工作正常我得到了这个文件:

代码语言:javascript
复制
-
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,我有以下几点:

代码语言:javascript
复制
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')

它在一开始工作得很好,但是突然它停止了工作,我得到了以下错误:

代码语言:javascript
复制
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)

我做了一些调试,发现

代码语言:javascript
复制
var i

变成0,那么什么都不能工作了(因为它必须跳过"-“字符)发生了什么,我该如何修复它?我已经尝试了很多,去掉了"-“字符,改变了nrow,但没有什么真正重要的(对meeeee来说)

希望有些人能帮上忙

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-01-27 22:53:59

这更像pythonic式的:

代码语言:javascript
复制
if ',' not in line:
    continue
name, stats = line.strip().split(',')
票数 1
EN

Stack Overflow用户

发布于 2019-01-27 20:23:32

这一行

代码语言:javascript
复制
name, stats = line.strip().split(',')

因为您的输入文件中有一行没有逗号,所以失败。而且它很可能在末尾是一个空行。做

代码语言:javascript
复制
columns = line.strip().split(',')
if len(columns) == 2:
    name, stats = columns
else:
    print("Expected name and stats, got", columns)
票数 2
EN

Stack Overflow用户

发布于 2019-01-27 20:25:07

试试这个:

代码语言:javascript
复制
if line.count(',') != 1:
    continue
name, stats = line.strip().split(',')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54388038

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档