python 读取excel文件 - 编码是个头疼的问题

问题:

如何将设计好的用例思维导图落地到RF中?

解决方案:

采用xmind生成了一个思维导图的图形
将该图形拷贝到excel表格中
读取该excel表,输出utf-8格式的txt,供robotframework调用

讨论:

1、没有考虑子层级不同的情况。

2、没有解决python读取excel时遇到的切换目录 、 创建txt文件时的 名称编码 问题。

3、面对复杂的,大量的用例情况下,比如多层嵌套,基本无能为力。     
4、只处理了框架,没有对各个用例对应的 输入 和输出 进行进一步的展示。其实excel除了CASE表格外,还有其他具体的表格展现。

5、没有考虑xmind下的用例关联和组合,目前只是手工保证了关联性,还没能借助工具展现。
                        
                        




# -*- coding:utf-8 -*-
'''
采用xmind生成了一个思维导图的图形
将该图形拷贝到excel表格中
读取该excel表,输出utf-8格式的txt,供robotframework调用
'''
import os
import sys
import xlrd
import json

##reload(sys)
##sys.setdefaultencoding('utf-8')
##print sys.getdefaultencoding()

def getXls(keyword):
        print u'获取文件'
        filenamelist = os.listdir(os.getcwd())
        for i in range (0,len(filenamelist)):
            if 'xls' in filenamelist[i]:
                #print filenamelist[i]
                filename = filenamelist[i]
                if keyword in filename:
                        #print u'找到了对应的excel'
                        return filename
        return False

def openXls(filename,sheetname):
        print u'获取对应的book和sheet,xlrd中没有关闭操作'
        workbook = xlrd.open_workbook(filename)
        shxrange = range(workbook.nsheets)
        try:
            worksheet = workbook.sheet_by_name(sheetname)
        except:
            print "no %s in %s " % (sheetname, filename)
            return False
        ncols = worksheet.ncols
        nrows = worksheet.nrows
        print u'SHEET: %s 的行数为 %d,列数为 %d' % (sheetname, nrows, ncols)
        return worksheet

def createDir(worksheet):
        print u'创建文件夹,目前只考虑两层目录,创建txt文件,utf-8格式'
        
        dir_path = dict()
        ncols = worksheet.ncols
        nrows = worksheet.nrows
        dir_name = worksheet
        dir_father = worksheet.cell_value(1,0)
        try:
                os.removedirs(dir_father)
        except:
                pass
        if not os.path.exists(dir_father):
                os.makedirs(dir_father)
        os.chdir(dir_father)
        #print(os.getcwd())
                
        for m in range(1,nrows-1):
                path_name = u''
                dir_son = worksheet.cell_value(m,1)
                if u'' != dir_son:
                        if not os.path.exists(dir_son):
                                os.makedirs(dir_son)
                        #print dir_son
                        path_name = dir_father + '\\' + dir_son
                        #print path_name
                        dir_path[path_name] = m
        dir_path = sorted(dir_path.items(), key=lambda dir_path:dir_path[1])
        dir_path1 =  json.dumps(dir_path, encoding="UTF-8", ensure_ascii=False)
        return dir_path

def createTxt(worksheet, dir_path):
        print u'创建txt文件,utf-8格式'
        dict_suite = dict()
        ncols = worksheet.ncols
        nrows = worksheet.nrows
        length = len(dir_path)
        
        txt_rows = []
        for i in range(0,length):
                txt_rows.append(dir_path[i][1])
        txt_rows.append(nrows)
        txt_len = len(txt_rows)

        dir_path_utf =  json.dumps(dir_path[0][0], encoding="UTF-8", ensure_ascii=False)
        print dir_path_utf

##        reload(sys)
##        sys.setdefaultencoding('utf-8')
##        print sys.getdefaultencoding()
        for i in range(0,txt_len-1):
                path = dir_path[i][0]
                rows_begin = txt_rows[i]
                rows_end = txt_rows[i+1]
                print path
##                os.chdir(path) #存在编码问题
                for r in range(rows_begin,rows_end-1):
                       if u'' != worksheet.cell_value(r,2):
                                suitename = worksheet.cell_value(r,2)
                               
                                
                                print '***'
                                print suitename
                                print type(suitename)
                                print suitename.encode('utf-8')
                                print type(suitename)
                                print suitename

                                suitename = u'%s.txt'%suitename
                                f = open(suitename, 'r+') #存在编码问题
                                


                



if __name__ == '__main__':
        keyword = u'UAPS'
        sheetname = u'CASE'
        filename = getXls(keyword)
        print filename
        if False != filename:
                worksheet = openXls(filename, sheetname)
                print worksheet
                if False != worksheet:
                        dir_path = createDir(worksheet)
                        print dir_path
                        createTxt(worksheet, dir_path)
                        
        
        


                        
                        


你可能感兴趣的:(python-自动化测试)