python實現xml和csv檔案轉化

2021-09-10 05:18:31 字數 3561 閱讀 3435

下面是轉抄的。稍微改了下,用在匯出的testcase xml檔案轉化成客戶要的csv檔案

# coding: utf-8

import sys

import importlib

importlib.reload(sys)

import csv

from xml.etree.elementtree import iterparse

import xml.etree.elementtree as et

from html.parser import htmlparser

spamwriter.writerow(suite_list)

if node.tag == "testcase":

case_list = ['testcase','','','','','','','','']

#print(node.attrib['internalid'])

print(node.attrib['name'])

case_list[1] = node.attrib['name']

#case_list[4] = node.attrib['internalid']

for child in node:

if child.tag == "summary":

print(self.strip_tags(str(child.text)))

case_list[2] = self.strip_tags(str(child.text))

if child.tag == "steps":

for child1 in child:

if child1.tag == "step":

for child2 in child1:

if child2.tag == "actions":

print(self.strip_tags(str(child2.text)))

case_list[3] = self.strip_tags(str(child2.text))

if child2.tag == "expectedresults":

print(self.strip_tags(str(child2.text)))

case_list[4] = self.strip_tags(str(child2.text))

spamwriter.writerow(case_list)

def read_csv_to_xml(self,csv_file,xmlfile):

#逐行讀取csv檔案的內容,將內容寫進以internalid為鍵,name,sumary,steps,expectresult為值得字典

csv_file = file(csv_file,'rb')

reader = csv.reader(csv_file)  

case_dic = {}  

for line in reader:  

if reader.line_num == 1:  

continue  

if line[0] == "testcase":

name = str(line[1])

internalid = str(line[4])

summary = line[6]

steps = line[7]

expectedresults = line[8]

case_dic[internalid] = (name,summary,steps,expectedresults)

csv_file.close()

print(case_dic)

#用elementtree方法開啟xml檔案,逐行解析xml檔案,發現case為tag的行,就將name,sumary,steps,expectresult,這幾項用字典的值替換。

tree = et.elementtree()

tree.parse(xmlfile)

root = tree.getroot()

root_suite_name = root.attrib['name']

for node in tree.iter():

if node.tag == "testsuite":

print(node.attrib['name'])

sub_suite_name = node.attrib['name']

if sub_suite_name == root_suite_name:

sub_suite_name = ""

for child in node:

if child.tag == "node_order":

#print child.text

pass

if child.tag == "details":

pass

if node.tag == "testcase":

new_internalid = node.attrib['internalid']

#將根目錄和子目錄的名字都寫進了case名字中。如果不需要可以用下面那行注釋掉的替換這一行

node.attrib['name'] = root_suite_name+'_'+sub_suite_name+'_'+case_dic[new_internalid][0]

#node.attrib['name'] = case_dic[new_internalid][0]

print(node.attrib['name'])

#解析tag為testcase的節點的子節點,並修改節點的值

for child in node:

if child.tag == "node_order":

pass

if child.tag == "externalid":

pass

if child.tag == "summary":

child.text = case_dic[new_internalid][1]

child.text = str(child.text.replace('\n',""))

if child.tag == "steps":

child.text = str(case_dic[new_internalid][2])

child.text = str(child.text.replace('\n',""))

if child.tag == "expectedresults":

child.text = case_dic[new_internalid][3]

child.text = str(child.text.replace('\n',""))

#將修改後的elementtree物件寫入xml檔案中。

tree.write(xmlfile,encoding='utf8')   

if __name__ == "__main__":

test = xml_csv()

test.read_xml_to_csv('testsuites2.csv','testsuites.xml')

#test.read_csv_to_xml('testsuites2.csv','testsuites.xml')

Python實現解析XML檔案

專案名收件人 負責人上面是測試的xml檔案。針對上述的格式xml,可以利用如下解析方法。coding utf 8 author yangxin ryan import xml.dom.minidom class xmlparser object def sql xml parser self,xml...

將CSV轉為XML的指令碼

1.csv是定義的比較簡單的 檔案格式,類似excel但是比它簡單 用文字編輯器開啟csv檔案,你可以看到 實際上基本都是用 來分割各個欄位的資料,如下 a,32,0,1,通訊站,交換機房.東門門磁 a,32,1,1,通訊站,交換機房.西門門磁 a,32,2,0,通訊站,交換機房.交流屏故障 a,3...

LINQ 從 CSV 檔案生成 XML

本文參考 下面的 對字串陣列執行 linq 查詢。在 c 版本中,該查詢使用 let 子句將每個字串分隔成欄位陣列。c vb create the text file.string csvstring greal,great lakes food market,howard snyder,marke...