python按行读取文件并找出其中指定字符串

#coding=utf-8

import os, time, sys, re
#reload(sys)
#sys.setdefaultencoding("utf8")   # 不设置,否则编码方式不对应,无法找出字符串

file = open(path)

sum=0
for line in file.readlines():

    #line = line.strip("\n")
    key = "解析渲染" 
    if key in line:
        s = re.findall('"TimeSpan":"([\d.]+)"', line)
        print "**************", line
        print "时间为:", s[-1]
        sum = sum + float(s[-1])
file.close()

print "总时间为:", sum

input("123")

注:print file.read()时会出现IOError[error 0],未知原因

你可能感兴趣的:(自动化,python)