testlink,csv格式转为xml ,以及xml转为csv 源代码

源码:

 
# -*- coding=gbk -*-
import sys
import csv
import xml.etree.ElementTree as ET
from HTMLParser import HTMLParser
from xml.dom.minidom import Document, Element

reload(sys)
sys.setdefaultencoding('gbk')


# 去掉xml文件中的HTML标签
def strip_tags(htmlStr):
    htmlStr = htmlStr.strip()
    htmlStr = htmlStr.strip("\n")
    result = []
    parser = HTMLParser()
    parser.handle_data = result.append
    parser.feed(htmlStr)
    parser.close()
    return ''.join(result)


def read_xml_to_csv(csv_file, xmlfile):
    # 打开并创建csv类型的文件
    global suite_list
    csvfile = open(csv_file, 'wb')
    spamwriter = csv.writer(csvfile, dialect='excel')
    # 创建csv表格的第一行

    spamwriter.writerow(
        ['组件标题', '用例标题', '版本', '摘要',
         '前提', '状态', '重要性',
         '测试执行时间', '步骤号', '步骤详细', '结果预期', ])
    # 异常处理
    try:
        tree = ET.ElementTree(file=xmlfile)  # 打开xml文档
    except IndexError, e:
        print("Error;cannot parse file")
        print e
        sys.exit(1)

    # 第一种方法,获取指定tag的text或attrib
    # for elem in tree.iter():
    #   if elem.tag == "xxx":
    #      print elem.text
    #      print elem.attrib[a]
    # 记录testsuite的个数
    i = 0
    m = 0
    for elem in tree.iter(tag='testsuite'):
        print elem.attrib
        i = i + 1
        print "testsuite count : " + str(i)
    for elem in tree.iter():
        if elem.tag == 'testsuite' and m == 0:
            suite_list = ['' for q in range(11)]
            print suite_list
            m = 1 + m
        if elem.tag == 'testsuite' and m != 0 :
            print 'testsuite is :' + elem.attrib['name']
            suite_list[0] = elem.attrib['name']
        if elem.tag == "testcase":
            print 'case_name is :' + elem.attrib['name']
            suite_list[1] = strip_tags(str(elem.attrib['name']))
        if elem.tag == "version":
            print "version is :" + elem.text
            suite_list[2] = strip_tags(str(elem.text))
        if elem.tag == "summary":
            print "summary is :" + strip_tags(str(elem.text))
            suite_list[3] = strip_tags(str(elem.text))
        if elem.tag == "preconditions":
            print "preconditions is :" + strip_tags(str(elem.text))
            suite_list[4] = strip_tags(str(elem.text))
        if elem.tag == "execution_type"  :
            print "execution_type is :" + strip_tags(str(elem.text))
            print "m:" + str(m)
            suite_list[5] = elem.text
        if elem.tag == "importance":
            print "importance is :" + strip_tags(str(elem.text))
            suite_list[6] = strip_tags(str(elem.text))
        if elem.tag == "estimated_exec_duration":
            print "estimated_exec_duration is :" + strip_tags(str(elem.text))
            suite_list[7] = strip_tags(str(elem.text))
        if elem.tag == "step_number":
            print "step_number is :" + strip_tags(str(elem.text))
            suite_list[8] = strip_tags(str(elem.text))
        if elem.tag == "actions":
            print "actions is :" + strip_tags(str(elem.text))
            suite_list[9] = strip_tags(str(elem.text))
        if elem.tag == "expectedresults":
            print "expectedresults is :" + strip_tags(str(elem.text))
            suite_list[10] = strip_tags(str(elem.text))


    # 第二种方法
    # for elem in tree.iter(tag='xxx'):
    #      print elem.text
    # for elem in tree.iter(tag='execution_type'):
    #     print "step_execution_type is :"+elem.text
    #     suite_list[20] = elem.text
    # 将值写入csv文件中
    spamwriter.writerow(suite_list)
    # 关闭csv文件
    csvfile.close()


def read_csv_to_xml(csv_file, xmlfile):
    
    xml_list = ['name', 'name', 'version', 'summary', 'preconditions', 'execution_type', 'importance', 'estimated_exec_duration', 'step_number', 'actions', 'expectedresults']
    # 以行为单位存储csv内容:
    global step
    with open(csv_file, 'rb') as csvfile:
        reader = csv.reader(csvfile)
        rows = [row for row in reader]

    # 在步骤一栏加入换行符
    for br in range(1, len(rows)):
        Rows = rows[br][5].splitlines()
        if len(Rows) == 1:
            rows[br][5] = "<p>"+Rows[0]+"</p>"

        if len(Rows) == 2:
            rows[br][5] = "<p>"+Rows[0]+"<br/>" + Rows[1]+"</p>"
        if len(Rows) >= 3:
            action = "<p>" + Rows[0]
            for R in range(1, len(Rows)):
                action = action+"<br/>" + Rows[R]
            action = action+"</p>"
            rows[br][5] = action
    # 在结果一栏加入换行符
    for br in range(1, len(rows)):

        Rows = rows[br][6].splitlines()
        if len(Rows) == 1:
            rows[br][6] = "<p>"+Rows[0]+"</p>"

        if len(Rows) == 2:
            rows[br][6] = "<p>"+Rows[0]+"<br/>" + Rows[1]+"</p>"

        if len(Rows) >= 3:
            action = "<p>" + Rows[0]
            for R in range(1, len(Rows)):
                action = action+"<br/>" + Rows[R]
            action = action+"</p>"
            rows[br][6] = action

    # 记录testsuite的种数
    suite_count = 0
    for j in range(1, len(rows)):
        if j == 1:
            value = rows[j][0]
            suite_count = suite_count + 1
        if not value == rows[j][0]:
            value = rows[j][0]
            suite_count = suite_count + 1

    # 记录有几种testsuite
    suit = [rows[1][0]]
    for le in range(1, len(rows)):
        if rows[le][0] in suit:
            pass
        if not rows[le][0] in suit:
            suit.append(rows[le][0])

    # 将suit一列加到test_case列表里
    test_suit = []
    for le in range(1, len(rows)):
        test_suit.append(rows[le][0])

    # 记录有几种testcase
    case = [rows[1][1]]
    for le in range(1, len(rows)):
        if rows[le][1] in case:
            pass
        if not rows[le][1] in case:
            case.append(rows[le][1])

    # 将case一列加到test_case列表里
    test_case = []
    for le in range(1, len(rows)):
        test_case.append(rows[le][1])

    # 在内存中创建一个空的文档
    doc = Document()

    # 创建一个根节点Managers对象
    Root = doc.createElement('testsuite')

    # 设置根节点的属性
    Root.setAttribute('name', '家长端')

    # 将根节点添加到文档对象中
    doc.appendChild(Root)

    for j in range(1, suite_count+1):

        # 计算当前suite下的case数量
        for num in range(1, len(rows)):
            if num == 1 :
                case_count = 0
            if rows[num][0] == suit[j-1] :
                case_count = case_count+1

        index = test_suit.index(suit[j-1])+1

        # 创建次节点
        locals()['root_' + str(j)] = doc.createElement('testsuite')

        # 设置次节点的属性
        locals()['root_' + str(j)].setAttribute(xml_list[0], suit[j-1])
        Root.appendChild(locals()['root_'+str(j)])

        for n in range(0, case_count):
            # 设置次子节点的属性
            locals()['nodeManager_'+str(n)] = doc.createElement('testcase')

            locals()['nodeManager_' + str(n)].setAttribute(xml_list[1], rows[index+n][1])

            # 将各叶子节点添加到父节点nodeManager_three中
            locals()['node'+str(n)] = doc.createElement(xml_list[2])
            # 设置节点为version的text值为
            locals()['node' + str(n) ].appendChild(doc.createTextNode("1"))

            locals()['nodeManager_' + str(n)].appendChild(locals()['node' + str(n)])
            for m in range(0, 2):
                locals()['node' + str(n) + str(m)] = doc.createElement(xml_list[m + 3])
                locals()['node' + str(n) + str(m)].appendChild(doc.createTextNode(rows[index+n][m+2]))
                locals()['nodeManager_' + str(n)].appendChild(locals()['node'+str(n)+str(m)])
            for m in range(0, 3):
                locals()['node' + str(n) + str(m)] = doc.createElement(xml_list[m + 5])
                locals()['node' + str(n) + str(m)].appendChild(doc.createTextNode("2"))
                locals()['nodeManager_' + str(n)].appendChild(locals()['node'+str(n)+str(m)])

            locals()['node'+str(n)] = doc.createElement('steps')
            locals()['step'+str(n)] = doc.createElement('step')  # type: Element
            locals()['nodeManager_' + str(n)].appendChild(locals()['node'+str(n)])
            locals()['node' + str(n)].appendChild(locals()['step'+str(n)])

            for a in range(0, 3):

                locals()['s'+str(a) + str(n)] = doc.createElement(xml_list[a+8])
                print index+n
                print a+4
                locals()['s' + str(a) + str(n)].appendChild(doc.createTextNode(rows[index+n][a+4]))
                locals()['step' + str(n)].appendChild(locals()['s'+str(a) + str(n)])
                locals()['root_' + str(j)].appendChild(locals()['nodeManager_' + str(n)])
            # 最后将Manager添加到根节点'root_' + j中

    # 开始写xml文档
    fp = open(xmlfile, 'w')
    doc.writexml(fp, indent='\n', addindent='\t', newl='\n', encoding="gbk")


if __name__ == "__main__":

    # read_xml_to_csv('testsuite-deep.csv', 'testsuites (2).xml')
    read_csv_to_xml('testsuite-deep.csv', 'testsuites.xml')

csv原文件示意图:


csv转xml结果示意图:


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值