ITeye博客

python 练手

youngerblue · 2012-05-26 21:08 · python · 编程语言 · Markdown

周末无聊拿起很久之前买的那本 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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。