2014-03-31 27 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'永遠不會匹配任何東西。 –