python xml 转换csv ,csv 转换xml

这是一个Python脚本,实现了将CSV文件转换为XML格式,并将XML文件转换回CSV的功能。脚本中定义了两个主要的转换函数:csv2full()和xml2csv(),以及用于检查路径、ID和目的正确性的辅助函数。它还包含了用于格式化日志输出的自定义颜色格式化器。脚本适用于自动化测试场景,可以生成符合特定结构的XML测试定义文件。
摘要由CSDN通过智能技术生成
#!/usr/bin/python
#encoding:utf-8


import os
import csv
import re
import sys
import platform
import logging  
import logging.handlers
 


from xml.etree import ElementTree


LOG = None
LOG_LEVEL = logging.DEBUG


class ColorFormatter(logging.Formatter):


    def __init__(self, msg):
        logging.Formatter.__init__(self, msg)


    def format(self, record):
        red, green, yellow, blue = range(4)
        colors = {'INFO': green, 'DEBUG': blue,
                  'WARNING': yellow, 'ERROR': red}
        msg = record.msg
        if msg[0] == "+":
            msg = "\33[01m" + msg[1:] + "\033[0m"
        elif msg[0] == "=":
            msg = "\33[07m" + msg + "\033[0m"
        levelname = record.levelname
        if levelname in colors:
            msg_color = "\033[0;%dm" % (
                31 + colors[levelname]) + msg + "\033[0m"
            record.msg = msg_color


        return logging.Formatter.format(self, record)






def csv2full(csv_path, split_sign):
    if not os.path.isfile(csv_path):
        print '%s is not a file' % csv_path
        return
    name, ext = os.path.splitext(csv_path)
    if not ext == '.csv':
        print '%s is not a csv' % csv_path
        return
#    print '== Convert csv to xml start=='
    LOG.info("+Convert csv to xml start ...")
    csv_file = file(csv_path, 'rb')
    csv_file.readline()
    reader = csv.reader(csv_file)
    csv_content = []
    for line in reader:
        csv_content.append(line)


    csv_file.close()
    suite_name = csv_content[0][16].split('/')[2]
    category_name = csv_content[0][12]
    set_name = name.split(split_sign)[-1]
    folder = os.path.dirname(csv_path)
#    folder = os.getcwd()
    full_test_path = '%s%stests.full(%s).xml' % (folder, split_sign, set_name)
    make_full_test(csv_content, full_test_path, suite_name, set_name, category_name)
#    print 'General %s' % full_test_path
    LOG.info('General %s' % full_test_path)
    test_path = '%s%stests(%s).xml' % (folder, split_sign, set_name)
    make_test(csv_content, test_path, suite_name, set_name, category_name)
#    print 'General %s' % test_path
    LOG.info('General %s' % test_path)
#    print '== Convert csv to xml finish=='
    LOG.info("== Convert csv to xml finish==")




def make_full_test(csv_content, full_test_name, suite_name, set_name, category_name):
    full_test_file = open(full_test_name, 'w')
    content = '<?xml version="1.0" encoding="UTF-8"?>\n<?xml-stylesheet type="text/xsl" href="./testcase.xsl"?>\n<test_definition>\n  <suite name="%s" launcher="WRTLauncher" category="%s">\n    <set name="%s">' % (suite_name, category_name, set_name)
    for line in csv_content:
        content += '\n      <testcase purpose="%s" type="%s" status="%s" component="%s" execution_type="%s" priority="%s" id="%s">\n        <description>\n          <test_script_entry>%s</test_script_entry>\n        </description>\n        <specs>\n          <spec>\n            <spec_assertion element_type="%s" element_name="%s" interface="%s" specification="%s" section="%s" category="%s"/>\n            <spec_url>%s</spec_url>\n            <spec_statement/>\n          </spec>\n        </specs>\n      </testcase>' % (line[1],
         line[17],
         line[15],
         line[2],
         line[4],
         line[6],
         line[0],
         line[16],
         line[7],
         line[8],
         line[9],
         line[10],
         line[11],
         line[12],
         line[13])


    content += '\n    </set>\n  </suite>\n</test_definition>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值