python(windows)中的值解包错误

2024-03-29 09:35:53 发布

您现在位置:Python中文网/ 问答频道 /正文

以下代码在ubuntu发行版上运行正常,但在windows中生成了一个错误

ValueError: not enough values to unpack (expected 3, got 2)

import sys
strcmd = "curve -d 3"
f = open("cam_data.dex","r")
for line in f:
 (x,y,z)=line.split(",")
 strcmd = strcmd+" -p"+" "+x+" "+y+" "+z.rstrip()

print(strcmd)
print("\nDONE\n")

我无法找到什么错误是任何帮助,这将是非常有用的


Tags: to代码ubuntuwindows错误linenotexpected
2条回答

问题:

(x,y,z)=line.split(",")这将返回一个不能映射到三个变量的列表

固定代码

以下是您可以做的:
import sys
strcmd = "curve -d 3"
f = open("cam_data.dex","r")
for line in f:
    x=" ".join(line.split(","))
    strcmd = strcmd+" -p"+" "+x

print(strcmd)
print("\nDONE\n")

输出:

curve -d 3 -p 1 2 3 3 2 1 4 5 6 6 5 4 7 8 9 9 8 7

您需要根据分隔符对split执行f,并对其进行迭代。你知道吗

import sys
strcmd = "curve -d 3"
f = open("cam_data.dex","r")
for line in f.split():
    (x,y,z)=line.split(",")
    strcmd = strcmd+" -p"+" "+x+" "+y+" "+z.rstrip()

print(strcmd)
print("\nDONE\n")
# curve -d 3 -p 1 2 3 -p 3 2 1 -p 4 5 6 -p 6 5 4 -p 7 8 9 -p 9 8 7                                                    
# DONE                    

相关问题 更多 >