Python 值错误
Python Value error
尝试将管道输出拆分为 python 时出现错误。
错误是 need more than 3 values to unpack
虽然我使用了 8 个值
import subprocess, sys
from datetime import datetime
from time import sleep as sleep
multimon_ng = subprocess.Popen("multimon-ng -a FLEX -t wav flex.wav",
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True)
while True:
nextline = multimon_ng.stdout.readline()
flex, mdate, mtime, bitrate, other, capcode, o2, msg = nextline.split(" ", 7) # error here
if nextline is " ":
print "napping"
else:
print mdate + " " + mtime + " " + capcode + " " + msg
multimon_ng.poll()
sys.stdout.flush()
任何帮助都会很棒
错误消息中的 3
指示右侧参数中可迭代的长度。
最少的错误示例:
a, = [] # ValueError: need more than 0 values to unpack
a, b = [1] # ValueError: need more than 1 value to unpack
a, b, c = [1, 2] # ValueError: need more than 2 values to unpack
# etc ...
最少的正确示例:
a, = [1]
a, b = [1, 2]
a, b, c = [1, 2, 3]
修复和暴露问题的最小变化是将解包迭代包装在 try-except 块中。
while True:
nextline = multimon_ng.stdout.readline()
if not nextline:
print "napping"
else:
try:
flex, mdate, mtime, bitrate, other, capcode, o2, msg = nextline.split(" ", 7)
except ValueError:
print "invalid line", nextline
else:
print mdate + " " + mtime + " " + capcode + " " + msg
multimon_ng.poll()
sys.stdout.flush()
如您所见,我还移动了解包前的空行检查。如果行为空,解包也会失败。
尝试将管道输出拆分为 python 时出现错误。
错误是 need more than 3 values to unpack
虽然我使用了 8 个值
import subprocess, sys
from datetime import datetime
from time import sleep as sleep
multimon_ng = subprocess.Popen("multimon-ng -a FLEX -t wav flex.wav",
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True)
while True:
nextline = multimon_ng.stdout.readline()
flex, mdate, mtime, bitrate, other, capcode, o2, msg = nextline.split(" ", 7) # error here
if nextline is " ":
print "napping"
else:
print mdate + " " + mtime + " " + capcode + " " + msg
multimon_ng.poll()
sys.stdout.flush()
任何帮助都会很棒
3
指示右侧参数中可迭代的长度。
最少的错误示例:
a, = [] # ValueError: need more than 0 values to unpack
a, b = [1] # ValueError: need more than 1 value to unpack
a, b, c = [1, 2] # ValueError: need more than 2 values to unpack
# etc ...
最少的正确示例:
a, = [1]
a, b = [1, 2]
a, b, c = [1, 2, 3]
修复和暴露问题的最小变化是将解包迭代包装在 try-except 块中。
while True:
nextline = multimon_ng.stdout.readline()
if not nextline:
print "napping"
else:
try:
flex, mdate, mtime, bitrate, other, capcode, o2, msg = nextline.split(" ", 7)
except ValueError:
print "invalid line", nextline
else:
print mdate + " " + mtime + " " + capcode + " " + msg
multimon_ng.poll()
sys.stdout.flush()
如您所见,我还移动了解包前的空行检查。如果行为空,解包也会失败。