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

相关文章

python最长回文串算法

给定一个字符串,要求在这个字符串中找到符合回文性质的最长子串。所谓回文性是指诸如 “aba”,"ababa","abba"这类的字符串,当然单个字符以及两个相邻相同字符也满足回文性质。...

python实现在一个画布上画多个子图

python实现在一个画布上画多个子图

matplotlib 是可以组合许多的小图, 放在一张大图里面显示的. 使用到的方法叫作 subplot. 均匀画图 使用import导入matplotlib.pyplot模块, 并简写...

使用wxpython实现的一个简单图片浏览器实例

上次我爬了n多图片,但是浏览的时候有一个问题。 图片浏览器的浏览一般都是按名称排的,而我对图片的命名是按照数字递增的。比如3总是会排在10后面,也就无法快速地浏览图片了。 所以,出于方便...

Python中一般处理中文的几种方法

Python中的中文是个很头痛的问题,Python2和Python3都会出现,而且py2中出现的概率要大些。  有一道面试题: Python中如何处理中文问题,能想到的就是以下...

python决策树之CART分类回归树详解

python决策树之CART分类回归树详解

决策树之CART(分类回归树)详解,具体内容如下 1、CART分类回归树简介   CART分类回归树是一种典型的二叉决策树,可以处理连续型变量和离散型变量。如...