我丢失了之前的帐号ID,所以我开始新的。
templist=[]
temps1=[]
templist2=[]
tempstat1={}
station1={}
station2={}
import os.path
def main():
#file name=animallog.txt
endofprogram=False
try:
filename=input("Enter name of input file >")
file=open(filename,"r")
except IOError:
print("File does not exist")
endofprogram=True
count=0
count2=0
for line in file:
line=line.strip('\n')
if (len(line)!=0)and line[0]!='#':
(x,y,z)=line.split(':')
templist.append((x,y,z))
record=(x,z)
temps1.append(record)
for x,y in record:
if x in station1 or station2:
if y=='s1':
station1[x]=station1[x]+1
elif y=='s2':
station2[x]=station2[x]+1
elif x not in station1 and station2:
if y=='s1':
station1[x]=1
elif y=='s2':
station2[x]=1
main()
无论如何,伙计们。我写了这个程序。它基本上是在读取一个包含如下信息的文件 - > (动物:日期:站号)
a01:01-24-2011:s1
a03:01-24-2011:s2
a03:09-24-2011:s1
我试图计算哪只动物去哪个站多少次。我不想要你的专家回答,但只需要知道这个错误意味着什么 -
File "/Applications/Wing101.app/Contents/MacOS/src/debug/tserver/_sandbox.py", line 58, in <module>
File "/Applications/Wing101.app/Contents/MacOS/src/debug/tserver/_sandbox.py", line 39, in main
builtins.ValueError: too many values to unpack (expected 2)
谢谢:)
修改 -
将for x,y in record:
更改为 for x in record:
但是当我尝试打印{}
和station1
时,它会打印station2
为什么要为station1
和station2
打印空白词典?
答案 0 :(得分:1)
record
是一个2元组的字符串。通过迭代指定2元组的record
,您试图将每个字符串拆分为2个变量。
除非它们是双字符字符串,否则无效。
也许您打算迭代temps1
?