pythonのトレーナー

1479 ワード

週末はつまらないです。昔買ったpythonの核心プログラムを取り上げました。翻訳してみたら、pythonはやはり簡潔で、javaよりずっと便利になりました。オンラインで簡単な業務機能を実行する必要があれば、直接にpythonファイルを書いて、オンラインに捨てて直接実行できます。
以下はログログを読み込み、先に記録し、引き続き改善中です。
# -*- coding: UTF-8 –*-
'''
Created on 2012-5-26

@author: lyh
'''
def loadfile():
    myfile = open("C:\Users\lyh\Desktop\log", "r")
    dict = {}
    for line in myfile:
        viewIndex = line.find("      ")
        if(viewIndex > 0):
            userIndex = line.find("  :")
            if (userIndex > 0):
                nameIndex = line.find(",IP")
                username = line[userIndex + 9:nameIndex]
                try:
                    if(username != "null"):
                        viewId = line[viewIndex + 21:]
                        if (username in dict):
                            dict[username] = dict[username] + "," + viewId
                        else:
                            dict[username] = viewId
                except Exception,e: 
                    print e
    myfile.close();  
                  
    for i in dict.keys():
        print 'key=%s,value=%s' % (i, dict[i])
                
            
if __name__ == '__main__':
    loadfile()
    pass