---
title: "python 练手"
author: "youngerblue"
date: "2012-05-26T21:08:33Z"
tags: []
categories: ["编程语言"]
slug: "python"
draft: false
lastmod: "2012-05-26T21:08:33Z"
id: 1541925
url: "https://youngerblue.iteye.com/blog/1541925"
---

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