2014-03-31 30 views
0

我失去了我以前的帐户ID,所以我开始新的。python太多的值来解压错误和空白输出

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) 

谢谢:)

EDIT--

改变for x,y in record:TOfor x in record:

但它打印{}当我尝试打印station1station2

为什么打印空白字典为station1station2

+1

你的问题到底是什么? – jdotjdot

+0

'和'和'或'不按照您认为的方式工作。他们在任何一方都需要一个布尔表达式。空字典总是评估“错误”,含有内容的字典(不管它们是什么)评估“真”。 –

回答

1

record是字符串的2元组。通过遍历record指定一个2元组,您试图将这些字符串中的每一个拆分为2个变量。

这不会工作,除非它们是双字符字符串。

也许你打算重写temps1而不是?

+0

好的!我现在明白了。 Temps1仅适用于去station1的动物。也许现在我已经有了解决方案。我需要改进我的代码! – Newbie

+0

正在打印的空白字典是否也是因为这个? – Newbie

+0

它们是空的,因为'y'永远不会匹配任何东西。 –