无法解压缩的值太多(预期为4) [英] Too many values to unpack (expected 4)

查看:225
本文介绍了无法解压缩的值太多(预期为4)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段基本代码,只是试图将一个文件分成4个列表. .txt文件的格式如代码下方所示,并用空格分隔每个数字.出现的错误是"ValueError:太多值无法解包(预期为4)".

I have this piece of basic code just trying to split a file into 4 lists. The .txt file is formatted as shown underneath the code, with spaces delimiting each number. The error appearing is 'ValueError: too many values to unpack (expected 4)'.

file = open("myfreefall.txt","r")
for line in file:
     if not line.startswith('#'):
         v,a,t,y= line.split(' ')


#v  a  t  y
-0.10 -9.81 0.01 5500.00
-0.20 -9.81 0.02 5500.00
-0.29 -9.81 0.03 5500.00
-0.39 -9.81 0.04 5499.99
-0.49 -9.81 0.05 5499.99
-0.59 -9.81 0.06 5499.99
-0.69 -9.81 0.07 5499.98
-0.78 -9.81 0.08 5499.97
...

但是,我将代码更改为如下所示,以测试它预期有多少个变量,并且错误更改为没有足够的值要解压(预期为5,得到4)".这没有意义,因为现在代码正在解压缩我想要的四个变量!任何解决方案/指针将不胜感激.

However I changed the code to be as shown below to test how many variables it was expecting, and the error changed to 'not enough values to unpack (expected 5, got 4)'. This doesn't make sense as now the code is unpacking the four variables I wanted! Any solutions/pointers would be much appreciated.

file = open("myfreefall.txt","r")
for line in file:
     if not line.startswith('#'):
         v,a,t,y,test= line.split(' ')

推荐答案

如注释者所建议,尝试打印该行以查看实际上引起问题的行(但请确保在line.split之前插入打印内容).

As commenters have suggested, try printing the line to see the line that's actually causing a problem (but be sure to insert the print before the line.split).

最可能的问题是末尾的空行,它被视为换行符.如果是这样,最简单的解决方案可能是在条件上加上if语句:

The most likely problem is a blank line at the end, which is read as a newline character. If so the easiest solution is probably to tack on another condition to the if statement:

file = open("myfreefall.txt","r")
for line in file:
     if not line.startswith('#') and line != '\n':
     # or, more robust:
     if not line.startswith('#') and len(line) == 4:
         v,a,t,y= line.split(' ')

或尝试/例外:

file = open("myfreefall.txt","r")
for line in file:
    try:
       v,a,t,y= line.split(' ')
    except ValueError:
       continue
       # Skip over this line if the extraction fails

第二个if语句更健壮,因为它仅在要使用四个列表条目的情况下才会尝试提取四个变量.任何其他情况(要提取的条目更多或更少)将被忽略.您可以根据自己的需求进行调整,例如通过将其更改为"len(line)> = 4"(如果您还希望读取更长的行).

The second if-statement is more robust because it will only try to extract your four variables if there are exactly four list entries to work with. Any other case (more or fewer entries to extract from) will be ignored. You can tailor it a bit more to your needs, e.g. by changing it to 'len(line) >= 4' if you wanted to allow longer lines to be read as well.

说实话,我想不出try/except变体的特殊优势,但是由于我只是写了出来,所以我不妨保留它.实际上,它可能有点过于强大,因为它还会跳过由于 other 的原因而导致ValueErrors的行,而不是要处理的条目太少.

To be honest, I can't think of a particular advantage for the try/except variant, but since I just wrote it out I might as well leave it in. It might be a bit too robust, in fact, because it would also skip lines that cause ValueErrors for reasons other than there being too few entries to work with.

您描述的第二次尝试失败是很有意义的. 您要告诉python将四个条目的列表分成五个变量,即v,a,t,y和test.这就是错误所指的内容,即预期5,得到4".您的错误行为可能已更改,因为该代码现在早已失败了很多.在它适用于除最后一行(仅一个列表项)外的所有行之前,它都适用.更改为五个变量后,第一行就会出现错误,该行有四个条目,但现在您要输入五个.

Your second attempt failing as you describe makes perfect sense. You're telling python to split a list of four entries into five variables, v, a, t, y and test. That's what the error is referring to when it says, 'expected 5, got 4'. The behaviour of your error probably changed because the code fails a lot sooner now. Before it was working for all lines except the last one, where there's only one list entry to use. With your change to five variables it's causing an error with the very first line, where there are four entries but now you're asking for five.

这篇关于无法解压缩的值太多(预期为4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆