python解析文件示例_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

时间: 2024-08-03 20:42:21

python解析文件示例_python的相关文章

python代码制作configure文件示例_python

在lua中,一直用lua作为config文件,或承载数据的文件 - 好处是lua本身就很好阅读,然后无需额外写解析的代码,还支持在configure文件中读环境变量,条件判断等. 在lua中通过loadfile, setfenv实现) python: cat config.py bar = 10 foo=100 cat python_as_config.py: ns = {} execfile('config.py', ns) print "\n".join(sorted(dir(ns

Python中使用dom模块生成XML文件示例_python

在Python中解析XML文件也有Dom和Sax两种方式,这里先介绍如何是使用Dom解析XML,这一篇文章是Dom生成XML文件,下一篇文章再继续介绍Dom解析XML文件. 在生成XML文件中,我们主要使用下面的方法来完成. 主要方法 1.生成XML节点(node) 复制代码 代码如下: createElement("node_name") 2.给节点添加属性值(Attribute) 复制代码 代码如下: node.setAttribute("att_name",

Python中使用第三方库xlutils来追加写入Excel文件示例_python

目前还没有更好的方法来追写Excel,lorinnn在网上搜索到以及之后用到的方法就是使用第三方库xlutils来实现了这个功能,主体思想就是先复制一份Sheet然后再次基础上追加并保存到一份新的Excel文档中去. 使用xlutils 代码实现如下: # -*- coding: utf-8 -*- ''' Created on 2012-12-17 @author: walfred @module: XLRDPkg.write_append @description: ''' import o

python删除文件示例分享_python

删除文件 复制代码 代码如下: os.remove(   filename )   # filename: "要删除的文件名" 产生异常的可能原因: (1)filename 不存在(2)对filename文件, 没有操作权限或只读. 删除文件夹下所有文件和子文件夹 : 复制代码 代码如下: import os  def delete_file_folder(src):      '''delete files and folders'''     if os.path.isfile(s

Python中使用第三方库xlrd来写入Excel文件示例_python

继上一篇文章使用xlrd来读Excel之后,这一篇文章就来介绍下,如何来写Excel,写Excel我们需要使用第三方库xlwt,和xlrd一样,xlrd表示read xls,xlwt表示write xls,同样目前版本只支持97-03版本的Excel.xlwt下载:xlwt 0.7.4 安装xlwt 安装方式一样是python setup.py install就可以了,或者直接解压到你的工程目录中. API介绍 获取一个xls实例 复制代码 代码如下: xls = ExcelWrite.Work

python读取html中指定元素生成excle文件示例_python

Python2.7编写的读取html中指定元素,并生成excle文件 复制代码 代码如下: #coding=gbkimport stringimport codecsimport os,timeimport xlwtimport xlrdfrom bs4 import BeautifulSoup from xlrd import open_workbook class LogMsg:        def __init__(self,logfile,Level=0):              

python使用scrapy解析js示例_python

复制代码 代码如下: from selenium import selenium class MySpider(CrawlSpider):    name = 'cnbeta'    allowed_domains = ['cnbeta.com']    start_urls = ['http://www.jb51.net']     rules = (        # Extract links matching 'category.php' (but not matching 'subse

python操作xml文件示例_python

复制代码 代码如下: def get_seed_data(filename):dom = minidom.parse(filename)root = dom.documentElementsystem_nodes = root.getElementsByTagName("system")k = 0seed_list = []for system_node in system_nodes:    #print system_node.nodeName+' id='+system_node

python合并文本文件示例_python

python实现两个文本合并 employee文件中记录了工号和姓名 复制代码 代码如下: cat employee.txt:100 Jason Smith200 John Doe300 Sanjay Gupta400 Ashok Sharma bonus文件中记录工号和工资 复制代码 代码如下: cat bonus.txt:100 $5,000200 $500300 $3,000400 $1,250 要求把两个文件合并并输出如下, 处理结果: 复制代码 代码如下: 400 ashok shar