当前访客身份:游客 [ 登录 | 加入 OSCHINA ]

代码分享

当前位置:
代码分享 » Python  » 编程基础
分享到: 
收藏 +0
1
由于有多线程的影响,c++项目打印出来的时间顺序不一致,导致不太好在excel中统计,故使用python写了段脚本来解决之。涉及到如下方面
1. txt文本的读取,utf8的处理
2. 字符串的基本操作
3. dict的基本操作
4.list(数组)的基本操作
标签: 文本文件 Python

代码片段(2) [全屏查看所有代码]

1. [代码][Python]代码     跳至 [1] [2] [全屏预览]

#!/usr/bin/python
#print "Hello World"
str_seperator = "=================================================================================="
timePointName = ["enter OpenNextImage at",#0
                 "enter OpenImage at",#1
                 "In OpenImage send On_ImageRefresh at",#2
                 "leave OpenImage at",#3
                 "leave OpenNextImage at",#4
                 "enter LoadImage at",#5
                 "decode began at",#6
                 "enter DrawClient at",#7
                 "leave DrawClient at",#8
                 "decode end at",#9
                 "in LoadImage send On_ImageRefresh at",#10
                 "leave loadImage at",#11
                 "second enter DrawClient at",#12
                 "second leave DrawClient at" #13
                 ]

itemNumber= 0;
avgTotal = 0; #13-0
avgFirstDraw = 0; #8-2
avgLoadImage = 0; #11-5
avgSecondDraw = 0;#13-10

fobj = open("F:\log.txt","r")
imageTimeSta = {}
dic = {}
path = ""
idx = 0
for line in fobj:
        idx = idx + 1
        if idx == 1:
                line = line[3:]
        else:
                pass
        
        line = line.strip()
        line = line.decode("utf-8").encode("gbk")
        if line == str_seperator:
                if path == "":
                        pass
                else:
                        imageTimeSta[path] = dic
                dic = {}
                path = ""
                continue

        tabIndex = line.find('\t')
        if tabIndex == -1:
                path = line
                print path
                continue

        tabLastIndex = line.rfind('\t')
        name = line[0:tabIndex]
        time = int(line[tabLastIndex + 1:])
        if name in dic:
                dic["second " + name] = time
        else:
                dic[name] = time

fobj.close()
itemNumber = len(imageTimeSta)
keys = imageTimeSta.keys();
for (k,dic) in imageTimeSta.iteritems():
        avgTotal += dic[timePointName[13]] - dic[timePointName[0]];
        avgFirstDraw += dic[timePointName[8]] - dic[timePointName[2]];
        avgLoadImage += dic[timePointName[11]] - dic[timePointName[5]];
        avgSecondDraw += dic[timePointName[13]] - dic[timePointName[10]];

print 'avgTotal',avgTotal / float(itemNumber)
print 'avgFirstDraw',avgFirstDraw / float(itemNumber)
print 'avgLoadImage',avgLoadImage / float(itemNumber)
print 'avgSecondDraw',avgSecondDraw / float(itemNumber)

#print imageTimeSta

2. [文件] log.txt ~ 6KB     下载(151)     跳至 [1] [2] [全屏预览]

enter OpenNextImage at		5124
D:\pics\测试图片\解码性能对比用图\jpeg\较小图\2.JPG
enter OpenImage at		5124
In OpenImage send On_ImageRefresh at		5124
enter LoadImage at		5124
leave OpenImage at		5124
leave OpenNextImage at		5124
decode began at		5124
enter DrawClient at		5140
leave DrawClient at		5155
decode end at		5265
in LoadImage send On_ImageRefresh at		5265
leave loadImage at		5265
enter DrawClient at		5280
leave DrawClient at		5327
==================================================================================
enter OpenNextImage at		6280
D:\pics\测试图片\解码性能对比用图\jpeg\较小图\3.JPG
enter OpenImage at		6280
In OpenImage send On_ImageRefresh at		6280
enter LoadImage at		6280
leave OpenImage at		6296
leave OpenNextImage at		6296
decode began at		6296
enter DrawClient at		6296
leave DrawClient at		6312
decode end at		6437
in LoadImage send On_ImageRefresh at		6437
enter DrawClient at		6437
leave loadImage at		6452
leave DrawClient at		6499
==================================================================================
enter OpenNextImage at		7265
D:\pics\测试图片\解码性能对比用图\jpeg\较小图\4.JPG
enter OpenImage at		7265
In OpenImage send On_ImageRefresh at		7265
leave OpenImage at		7265
leave OpenNextImage at		7265
enter LoadImage at		7265
decode began at		7265
enter DrawClient at		7265
leave DrawClient at		7296
decode end at		7421
in LoadImage send On_ImageRefresh at		7421
enter DrawClient at		7421
leave loadImage at		7437
leave DrawClient at		7483
==================================================================================
enter OpenNextImage at		8062
D:\pics\测试图片\解码性能对比用图\jpeg\较小图\5.JPG
enter OpenImage at		8062
In OpenImage send On_ImageRefresh at		8062
leave OpenImage at		8062
leave OpenNextImage at		8062
enter LoadImage at		8062
decode began at		8062
enter DrawClient at		8062
leave DrawClient at		8077
decode end at		8202
in LoadImage send On_ImageRefresh at		8202
enter DrawClient at		8202
leave DrawClient at		8265
leave loadImage at		8280
==================================================================================
enter OpenNextImage at		8811
D:\pics\测试图片\解码性能对比用图\jpeg\较小图\6.JPG
enter OpenImage at		8811
In OpenImage send On_ImageRefresh at		8811
leave OpenImage at		8811
leave OpenNextImage at		8811
enter LoadImage at		8811
decode began at		8811
enter DrawClient at		8811
leave DrawClient at		8843
decode end at		8968
in LoadImage send On_ImageRefresh at		8968
leave loadImage at		8968
enter DrawClient at		8968
leave DrawClient at		9030
==================================================================================
enter OpenNextImage at		9515
D:\pics\测试图片\解码性能对比用图\jpeg\较小图\7.JPG
enter OpenImage at		9515
In OpenImage send On_ImageRefresh at		9515
leave OpenImage at		9515
leave OpenNextImage at		9515
enter LoadImage at		9515
decode began at		9530
enter DrawClient at		9530
leave DrawClient at		9546
decode end at		9671
in LoadImage send On_ImageRefresh at		9671
enter DrawClient at		9671
leave loadImage at		9671
leave DrawClient at		9733
==================================================================================
enter OpenNextImage at		10171
D:\pics\测试图片\解码性能对比用图\jpeg\较小图\8.JPG
enter OpenImage at		10171
In OpenImage send On_ImageRefresh at		10171
leave OpenImage at		10171
leave OpenNextImage at		10171
enter LoadImage at		10171
decode began at		10186
enter DrawClient at		10186
leave DrawClient at		10202
decode end at		10311
in LoadImage send On_ImageRefresh at		10311
leave loadImage at		10311
enter DrawClient at		10311
leave DrawClient at		10374
==================================================================================
enter OpenNextImage at		10811
D:\pics\测试图片\解码性能对比用图\jpeg\较小图\9.JPG
enter OpenImage at		10811
In OpenImage send On_ImageRefresh at		10811
enter LoadImage at		10811
leave OpenImage at		10811
leave OpenNextImage at		10811
enter DrawClient at		10811
decode began at		10811
leave DrawClient at		10843
decode end at		10952
in LoadImage send On_ImageRefresh at		10952
leave loadImage at		10952
enter DrawClient at		10952
leave DrawClient at		11030
==================================================================================
enter OpenNextImage at		11452
D:\pics\测试图片\解码性能对比用图\jpeg\较小图\10.JPG
enter OpenImage at		11452
In OpenImage send On_ImageRefresh at		11452
leave OpenImage at		11452
leave OpenNextImage at		11452
enter LoadImage at		11452
decode began at		11452
enter DrawClient at		11468
leave DrawClient at		11483
decode end at		11593
in LoadImage send On_ImageRefresh at		11593
enter DrawClient at		11593
leave loadImage at		11608
leave DrawClient at		11655
==================================================================================
enter OpenNextImage at		12077
enter DrawClient at		12077
leave DrawClient at		12108
==================================================================================
enter OpenNextImage at		13124
D:\pics\测试图片\解码性能对比用图\jpeg\较小图\1.jpg
enter OpenImage at		13124
In OpenImage send On_ImageRefresh at		13124
leave OpenImage at		13124
leave OpenNextImage at		13124
enter LoadImage at		13124
decode began at		13124
enter DrawClient at		13139
leave DrawClient at		13155
decode end at		13358
in LoadImage send On_ImageRefresh at		13358
leave loadImage at		13358
enter DrawClient at		13358
leave DrawClient at		13405
==================================================================================


开源中国-程序员在线工具:Git代码托管 API文档大全(120+) JS在线编辑演示 二维码 更多»

发表评论 回到顶部 网友评论(8)

  • 1楼:云涵 发表于 2011-04-14 11:00 回复此评论
    這個有點複雜了,不符合python的簡洁特性.
    我覺得可以用將' =================================================================================='
    用'='*82表示.將整個文檔用這個長的'='分隔開split('='*82),這樣你可以得到一個list,包含多個圖片信息.
    再針對list中每個項目進行再次split('\n'),得到每個圖片每個屬性名稱及值.
    最后可以總整統計list的結果就可以了.
  • 2楼:云涵 发表于 2011-04-14 13:33 回复此评论
    我稍微改了一下.大致做了一些簡化.
    #!/usr/bin/python
    #coding=utf-8
    
    f=open("d:\log.txt","r")
    
    dl=''.join(f.readlines()).split('='*82)#根據長'='字串分隔成list
    
    f.close()
    
    d={}
    lc=0
    
    for p in dl: #遍歷list中每個圖片信息
       if p.strip().split('\n')[1].split('\\').__len__()==1:break #如果第二項不是一個圖片踴徑,就跳過
       for i in p.strip().split('\n'):#遍歷圖片信息中各項屬性
          if i.split('\t\t').__len__() <> 2:continue #如果不是屬性和值的表示就執行下一個循環
          p,v =i.split('\t\t') #分離每個屬性名稱及數據值
          p = p.lstrip('\xef\xbb\xbf')
          if d.has_key(p):
             d[p] = d[p] + float(v)
          else:
             d[p] = float(v)
       lc += 1 #記錄統次的圖片次數
          
    print d

  • 3楼:云涵 发表于 2011-04-14 13:35 回复此评论
    這個d中就是根據各個屬性值的一些統計,不過我看你的代碼中,有些屬性名稱加了'second ',不知道是表達什麼意思.
  • 4楼:speedmancs 发表于 2011-04-14 16:18 回复此评论
    多谢。向你学习,以后我会经常使用这个脚本工具,简洁性确实蛮重要的。
  • 5楼:syshack 发表于 2012-05-22 16:07 回复此评论

    引用来自“云涵”的评论

    這個d中就是根據各個屬性值的一些統計,不過我看你的代碼中,有些屬性名稱加了'second ',不知道是表達什麼意思.
    真漂亮的代码~
  • 6楼:马阳阳 发表于 2012-11-25 22:42 回复此评论

    引用来自“云涵”的评论

    我稍微改了一下.大致做了一些簡化.
    #!/usr/bin/python
    #coding=utf-8
    
    f=open("d:\log.txt","r")
    
    dl=''.join(f.readlines()).split('='*82)#根據長'='字串分隔成list
    
    f.close()
    
    d={}
    lc=0
    
    for p in dl: #遍歷list中每個圖片信息
       if p.strip().split('\n')[1].split('\\').__len__()==1:break #如果第二項不是一個圖片踴徑,就跳過
       for i in p.strip().split('\n'):#遍歷圖片信息中各項屬性
          if i.split('\t\t').__len__() <> 2:continue #如果不是屬性和值的表示就執行下一個循環
          p,v =i.split('\t\t') #分離每個屬性名稱及數據值
          p = p.lstrip('\xef\xbb\xbf')
          if d.has_key(p):
             d[p] = d[p] + float(v)
          else:
             d[p] = float(v)
       lc += 1 #記錄統次的圖片次數
          
    print d

    Python的方式,简洁优美。学习了。看高手的代码,收获颇多!
  • 7楼:chedaoqiaotou 发表于 2014-01-07 20:13 回复此评论

    引用来自“云涵”的评论

    我稍微改了一下.大致做了一些簡化.
    #!/usr/bin/python
    #coding=utf-8
    
    f=open("d:\log.txt","r")
    
    dl=''.join(f.readlines()).split('='*82)#根據長'='字串分隔成list
    
    f.close()
    
    d={}
    lc=0
    
    for p in dl: #遍歷list中每個圖片信息
       if p.strip().split('\n')[1].split('\\').__len__()==1:break #如果第二項不是一個圖片踴徑,就跳過
       for i in p.strip().split('\n'):#遍歷圖片信息中各項屬性
          if i.split('\t\t').__len__() <> 2:continue #如果不是屬性和值的表示就執行下一個循環
          p,v =i.split('\t\t') #分離每個屬性名稱及數據值
          p = p.lstrip('\xef\xbb\xbf')
          if d.has_key(p):
             d[p] = d[p] + float(v)
          else:
             d[p] = float(v)
       lc += 1 #記錄統次的圖片次數
          
    print d

    这个简化牛!而且带注释,一目了然。
  • 8楼:adameeee 发表于 2014-09-15 00:08 回复此评论

    引用来自“云涵”的评论

    我稍微改了一下.大致做了一些簡化.
    #!/usr/bin/python
    #coding=utf-8
    
    f=open("d:\log.txt","r")
    
    dl=''.join(f.readlines()).split('='*82)#根據長'='字串分隔成list
    
    f.close()
    
    d={}
    lc=0
    
    for p in dl: #遍歷list中每個圖片信息
       if p.strip().split('\n')[1].split('\\').__len__()==1:break #如果第二項不是一個圖片踴徑,就跳過
       for i in p.strip().split('\n'):#遍歷圖片信息中各項屬性
          if i.split('\t\t').__len__() <> 2:continue #如果不是屬性和值的表示就執行下一個循環
          p,v =i.split('\t\t') #分離每個屬性名稱及數據值
          p = p.lstrip('\xef\xbb\xbf')
          if d.has_key(p):
             d[p] = d[p] + float(v)
          else:
             d[p] = float(v)
       lc += 1 #記錄統次的圖片次數
          
    print d

    用codecs来open是不是会好点,win32下txt不注意的话可能会有乱码的问题呢
开源从代码分享开始 分享代码
speedmancs的其它代码 全部(6)...