2009-12-29 38 views
0
f = open('transaction.log','r') 

ClerkHash = dict() 
arr = [0,0] 

for line in f: 
    Tdate  = line[0:12] 
    AccountKey = line[12:50] 
    TransType = line[22:2] 
    ClerkKey  = line[24:10] 
    CurrencyCode = line[34:2] 
    Amount  = line[36:45] 
    print line 
    print '\n' 
    print AccountKey 
    print '\n' 
    print Tdate   print '\n' 

    if TransType=="04": 
     ClerkHash[ClerkKey+AccountKey] = arr; // is this line corrent ? i don't want to corrupt the array every time ? how should i do it ? 
     ClerkHash[ClerkKey+AccountKey][0]+=1 
     ClerkHash[ClerkKey+AccountKey][1]+= Amount 


for Key in ClerkHash.keys(): 
    if ClerkHash[key][0] >= 3 and ClerkHash[key][1] > 1000: 
     print Key 

我想有一個哈希名稱ClerkHash [ClerkKey + AccountKey] 其中2 INT的陣列的consistes:第一索引是戒斷NUM,並且第二個是ammount的 做我定義了數組和哈希好? 另外我想總結ammount ...我該怎麼做呢?蟒 - 問題INT /串和散列/陣列

+0

,你不應該做的'打印AccountKey'(而不是僅僅'AccountKey') –

+1

這將真正如果您指定的預期輸出會是什麼幫助。而是會發生什麼呢。 – Skurmedel

+0

行[22:2]不會返回任何內容,因爲切片的結束在開始之前。 – nosklo

回答

2

下面是幾個問題,我迄今所看到

Amount  = line[36:45] 

應該

Amount  = int(line[36:45]) 

ClerkHash[ClerkKey+AccountKey] = arr[0,0] 

應該

ClerkHash[ClerkKey+AccountKey] = [0,0] 
0

檢查您的切片間隔!第二個參數是另一個索引,而不是從第一個索引中採取的步驟數。我想

TransType = line[22:2] 

而應是

TransType = line[22:24] 

如果設置

ClerkHash[ClerkKey+AccountKey] = [0, 0] 
每次遇到 TransType == "04"時間

您覆蓋值。因此,改變

if TransType=="04": 
     ClerkHash[ClerkKey+AccountKey] = arr[0,0] 
     ClerkHash[ClerkKey+AccountKey][0]+=1 
     ClerkHash[ClerkKey+AccountKey][1]+= Amount 

if TransType=="04": 
    if not ClerkHash.has_key(ClerkKey+AccountKey): 
     ClerkHash[ClerkKey+AccountKey] = [1, Amount] 
    else: 
     ClerkHash[ClerkKey+AccountKey][0] += 1 
     ClerkHash[ClerkKey+AccountKey][1] += Amount