python解析文件示例

yipeiwu_com5年前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

相关文章

Django的分页器实例(paginator)

先导入模块: from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger 分页器paginat...

TensorFlow实现Softmax回归模型

一、概述及完整代码 对MNIST(MixedNational Institute of Standard and Technology database)这个非常简单的机器视觉数据集,T...

python3+PyQt5使用数据库表视图

python3+PyQt5使用数据库表视图

上文提到窗体可以一次性呈现出来自同一记录的各个域,但是对于用户希望能看到多条记录的表来说,就需要使用表格化的视图了。本文通过python3+pyqt5改写实现了python Qt gui...

深入理解Python单元测试unittest的使用示例

深入理解Python单元测试unittest的使用示例

软件测试 大型软件系统的开发是一个很复杂的过程,其中因为人的因素而所产生的错误非常多,因此软件在开发过程必须要有相应的质量保证活动,而软件测试则是保证质量的关键措施。正像软件熵(soft...

django实现登录时候输入密码错误5次锁定用户十分钟

django实现登录时候输入密码错误5次锁定用户十分钟

在学习django的时候,想要实现登录失败后,进行用户锁定,切记录锁定时间,在网上找了很多资料,但是都感觉不是那么靠谱,于是乎,我开始了我的设计,其实我一开始想要借助redis呢,但是想...