以下代码在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")
我无法找到错误是什么,对此有任何帮助将非常有帮助
答案 0 :(得分: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
答案 1 :(得分:0)
您需要在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