python解析文件示例

python最近的工作主要是组件兼容性测试,原有的框架有很多功能还不完善,需要补充!比如,需要将AutoIt脚本的执行结果写入到Excel中,最后的解决方案是使用本地的log来解析这个结果!

增加了如下一个类来完成上述功能:

代码如下:

class AutoItResultParser():
    def ParseResult(self, vm_result, log_file):
        for case_result in vm_result.cases_results:
            self.__ModifyAutoItResult(case_result, log_file)

def __ModifyAutoItResult(self, result, log_file):
        items = []
        myfile = open(log_file, 'rb')
        line = myfile.readline()
        count = 0
        while('' != line):
            items.append(line.split(':')[0])
            count += 1
            if(count % 2 == 0):
                items.append(line.split(':')[1])
            line = myfile.readline()

myfile.close()
        fail_scripts = []
        length = len(items)
        arr = list(range(2, length, 3))
        for i in arr:
            test = items[i].lower()
            if test.rfind('success') == -1:
                fail_scripts.append((items[i - 2], items[i - 1]))

for script in fail_scripts:
            if script[0] == result.case_name:
                if script[1] == 'Installation':
                    result.install_script_success = False
                elif script[1] == 'Launch':
                    result.launch_script_success = False
                elif script[1] == 'Function':
                    result.function_script_success = False
                else:
                    result.uninstall_script_success = False

这里的log_file文件内容类似如下:

代码如下:

VisualStudio2010_StandaloneProfiler:
Installation:   Success
VisualStudio2010_StandaloneProfiler:
Launch:         Success
VisualStudio2010_StandaloneProfiler:
Function:       Fail
TaobaoBrowser_2.0.0:
CitrixOfflinePlugin_6.5:
Installation:   Success
CitrixOfflinePlugin_6.5:
Function:       Success
TrusteerRapport:
TNTShippingTools:
Installation:   Success
TNTShippingTools:
Launch:         Success
WGET_1.11.4:
Installation:   Success
VisualStudio2010_StandaloneProfiler:
Uninstallation: Success
TNTShippingTools:
Uninstallation: Fail

(0)

相关推荐

  • python解析发往本机的数据包示例 (解析数据包)

    tcp.py 复制代码 代码如下: # -*- coding: cp936 -*-import socketfrom struct import *from time import ctime,sleepfrom os import system system('title tcp sniffer')system('color 05') # the public network interfaceHOST = socket.gethostbyname(socket.gethostname())

  • python实现dnspod自动更新dns解析的方法

    复制代码 代码如下: def ddns():"""用当前ip更新ddns"""headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/json"}conn = httplib.HTTPSConnection("dnsapi.cn", timeou

  • python实现的解析crontab配置文件代码

    #/usr/bin/env python #-*- coding:utf-8 -*- """ 1.解析 crontab 配置文件中的五个数间参数(分 时 日 月 周),获取他们对应的取值范围 2.将时间戳与crontab配置中一行时间参数对比,判断该时间戳是否在配置设定的时间范围内 """ #$Id $ import re, time, sys from Core.FDateTime.FDateTime import FDateTime def

  • python正则匹配抓取豆瓣电影链接和评论代码分享

    复制代码 代码如下: import urllib.requestimport reimport time def movie(movieTag): tagUrl=urllib.request.urlopen(url)    tagUrl_read = tagUrl.read().decode('utf-8')    return tagUrl_read def subject(tagUrl_read): '''         这里还存在问题:        ①这只针对单独的一页进行排序,而没有

  • python解析模块(ConfigParser)使用方法

    测试配置文件test.conf内容如下: 复制代码 代码如下: [first]w = 2v: 3c =11-3 [second] sw=4test: hello 测试配置文件中有两个区域,first和second,另外故意添加一些空格.换行. 下面解析: 复制代码 代码如下: >>> import ConfigParser>>> conf=ConfigParser.ConfigParser()>>> conf.read('test.conf')['te

  • python解析中国天气网的天气数据

    使用方法:terminal中输入 复制代码 代码如下: python weather.py http://www.weather.com.cn/weather/101010100.shtml 北京6天的天气数据 json格式 复制代码 代码如下: #coding=utf-8  #weather.py  import urllib  import re  import simplejson  import sys if len(sys.argv) != 2:      print 'please

  • python实现网页链接提取的方法分享

    复制代码 代码如下: #encoding:utf-8import socketimport htmllib,formatterdef open_socket(host,servname):    s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)    port=socket.getservbyname(servname)    s.connect((host,port))    return shost=''host=input('请输入网址\

  • Python解析网页源代码中的115网盘链接实例

    本文实例讲述了python解析网页源代码中的115网盘链接的方法.分享给大家供大家参考.具体方法分析如下: 其中的1.txt,是网页http://bbs.pediy.com/showthread.php?t=144788另存为1.txt 具体代码如下: import re if __name__ == "__main__": fp = open("c:\\1.txt") https = re.compile(r"(http://u.*)") fo

  • python基于xml parse实现解析cdatasection数据

    本文实例讲述了python基于xml parse实现解析cdatasection数据的方法,分享给大家供大家参考. 具体实现方法如下: from xml.dom.minidom import * implementation = DOMImplementation() print "Core:%s" % implementation.hasFeature('core', '2.0') print "Events:%s" % implementation.hasFea

  • Python实现抓取网页并且解析的实例

    本文以实例形式讲述了Python实现抓取网页并解析的功能.主要解析问答与百度的首页.分享给大家供大家参考之用. 主要功能代码如下: #!/usr/bin/python #coding=utf-8 import sys import re import urllib2 from urllib import urlencode from urllib import quote import time maxline = 2000 wenda = re.compile("href=\"htt

  • 使用python解析xml成对应的html示例分享

    SAX将dd.xml解析成html.当然啦,如果得到了xml对应的xsl文件可以直接用libxml2将其转换成html. 复制代码 代码如下: #!/usr/bin/env python # -*- coding: utf-8 -*-#---------------------------------------#   程序:XML解析器#   版本:01.0#   作者:mupeng#   日期:2013-12-18#   语言:Python 2.7#   功能:将xml解析成对应的html#

  • Python通过解析网页实现看报程序的方法

    本文所述实例可以实现基于Python的查看图片报纸<参考消息>并将当天的图片报纸自动下载到本地供查看的功能,具体实现代码如下: # coding=gbk import urllib2 import socket import re import time import os # timeout in seconds #timeout = 10 #socket.setdefaulttimeout(timeout) timeout = 10 urllib2.socket.setdefaulttim

  • python 解析html之BeautifulSoup

    复制代码 代码如下: # coding=utf-8 from BeautifulSoup import BeautifulSoup, Tag, NavigableString from SentenceSpliter import SentenceSpliter from os.path import basename,dirname,isdir,isfile from os import makedirs from shutil import copyfile import io import

随机推荐