2009-12-29 37 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