ValueError:需要超过0个值才能解压 [英] ValueError: need more than 0 values to unpack

查看:67
本文介绍了ValueError:需要超过0个值才能解压的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手,我正在尝试制作一个程序来读取文件,并将信息放入其自己的向量中.该文件是一个xyz文件,如下所示:

I am new to python and I am trying make a program that reads a file, and puts the information in its own vectors. the file is an xyz file that looks like this:

45 

Fe -0.055 0.033 -0.047
N -0.012 -1.496 1.451
N 0.015 -1.462 -1.372
N 0.000 1.386 1.481
N 0.070 1.417 -1.339
C -0.096 -1.304 2.825
C 0.028 -1.241 -2.739
C -0.066 -2.872 1.251
C -0.0159 -2.838 -1.205

从第三行开始,我需要将它们放置在自己的向量中,到目前为止,我已经做到了:

Starting from the 3rd line I need to place each in its own vectors, so far I have this:

file=open("Question4.xyz","r+")
A = []
B = []
C = []
D = []
counter=0
for line in file:
    if counter>2: #information on particles start on the 2nd line
        a,b,c,d=line.split()
        A.append(a)
        B.append(float(b))
        C.append(float(c))
        D.append(float(d))
    counter=counter+1

我收到此错误:

 File "<pyshell#72>", line 3, in <module>
    a,b,c,d=line.split()
ValueError: need more than 0 values to unpack

关于我要去哪里的任何想法吗?

Any ideas on where I am going wrong?

提前谢谢!

推荐答案

看来您的行实际上并没有导致拆分的4个项目.为此添加一个条件.

It looks like you have lines in your that doesn't actually result in 4 items on splitting. Add a condition for that.

for line in file:
    spl = line.strip().split()
    if len(spl) == 4:  # this will take care of both empty lines and 
                       # lines containing greater than or less than four items
        a, b, c, d = spl
        A.append(a)
        B.append(float(b))
        C.append(float(c))
        D.append(float(d))

这篇关于ValueError:需要超过0个值才能解压的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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