`
youngerblue
  • 浏览: 43418 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

python 练手

 
阅读更多

周末无聊拿起很久之前买的那本 python核心编程,翻了一下,感觉python果然简洁,比java方便多了。如果线上需要执行一个业务简单的小功能,完全可以直接写个python文件扔到线上直接执行。

下面是读取log日志的,先记录,继续改进中

# -*- 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
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics