[英]with python convert csv to xml file
I am trying to read csv file and convert to xml file using python version 3.8.我正在尝试读取 csv 文件并使用 python 版本 3.8 转换为 xml 文件。 I didn't get the proper solution after referring the link With PYTHON convert CSV file to XML file .
在参考链接With PYTHON convert CSV file to XML file后,我没有得到正确的解决方案。 I am appreciating your valuable help and guidelines.
感谢您提供宝贵的帮助和指导。
Input CSV file输入 CSV 文件
release,stage,label,jvm_version,os,testunit,testname,testpath
Training,8,MAIN,160_01,Linux,com.java.testunit,com.java.testunit.testcase1,com/sun/ts/tests/client.java
Training,8,MAIN,160_01,Linux,com.java.testunit,com.java.testunit.testcase2,com/sun/ts/tests/client1.java
Tried code and I am not sure, how test-case tag code block.试过的代码,我不确定,测试用例标签代码块如何。 Someone help to improve this code.
有人帮助改进此代码。
import pandas as pd
xml_map = {"test-result": []}
df = pd.read_csv("test.csv", dtype=str, keep_default_na=False, header=0)
print(df.columns)
outfile = open('output.xml', 'w')
def printXML(data, xmlm, level):
pass
print('<?xml version="1.0"?>', file=outfile)
print('<!DOCTYPE test-log PUBLIC "-//Oracle Corp.//DTD GTLF 1.0//EN" "http://sapphire.us.oracle.com/docs/downloads/gtlf-config-2-0.dtd">', file=outfile)
print(f"""<test-log
testtype="AUTOMATED"
release="{df.release.unique()[0]}"
load="1"
branch="{df.label.unique()[0]}"
string="4"
changenumber="20210303103457"
analyzer=""
hostname="adc00isy"
toptestfile="cts"
runmodifier=""
runid="raj_20220411_1">""", file=outfile)
print(f"""
<environment>
<env-attribute name="JVM_Version" value="{df.jvm_version.unique()[0]}"/>
<env-attribute name="Primary_Config" value="w2.jrf"/>
<env-attribute name="Secondary_Config" value="jrockit80.oracle"/>
<env-attribute name="OS" value="{df.os.unique()[0]}"/>
<env-attribute name="RunKey" value="W2K-cts5-appclient-jrockit-load"/>
<env-attribute name="NativeIO" value="true"/>
<env-attribute name="JVM_Mode" value="server"/>
<env-attribute name="JVM_Name" value="jrockit80"/>
</environment>
<header-info
execaccount="bt"
execdate="2021-03-03 10:34:57.685"
checksum="7005"
resultcount="7005"
harnesstype="cts"
importinfo="Import from cts"
testruntype="cts5"/>""", file=outfile)
print("</test-log>", file=outfile)
Expected XML File format预期 XML 文件格式
<?xml version="1.0"?>
<mytest-log
testtype="MANUAL"
release="Training"
branch="MAIN"
>
<environment>
<env-attribute name="jvm_version" value="160_02"/>
<env-attribute name="os" value="LINUX"/>
</environment>
<test-result
logicalname="java.testunit.testcase1">
<test-case
testcasename="java.testunit"
testunit="java.testunit.testcase1"
testpath="com/sun/ts/tests/client.java"
/>
<execution-output>
<output-details>
</output-details>
</execution-output>
</test-result>
<test-result
logicalname="java.testunit.testcase2">
<test-case
testcasename="java.testunit"
testunit="java.testunit.testcase2"
testpath="com/sun/ts/tests/client1.java"
/>
<execution-output>
<output-details>
</output-details>
</execution-output>
</test-result>
</mytest-log>
I have made the following code and working fine now.我已经制作了以下代码并且现在工作正常。
from textwrap import indent
import lxml.etree as ET
import pandas as pd
import datetime
import math
import random
df = pd.read_csv("test.csv", dtype=str, keep_default_na=False, header=0)
def random_string():
digits = range(10)
random_str = ""
for _ in range(6):
index = math.floor(random.random() * 10)
random_str += str(digits[index])
return random_str
def generateXML(data):
datetime_now = datetime.datetime.now()
root = ET.Element('mytest-log',
testtype="AUTOMATED",
release=f"{data.release.unique()[0]}",
branch=f"{data.label.unique()[0]}",
runid=f"AUTO_{random_string()}"
)
envNode = ET.SubElement(root, 'environment')
envSubNode = ET.SubElement(envNode, 'env-attribute', name='JVM_Version', value=f'{data.jvm_version.unique()[0]}')
envSubNode = ET.SubElement(envNode, 'env-attribute', {'name': 'OS', 'value': f'{data.os.unique()[0]}'})
for index, row in data[['testunit', 'testname', 'testpath']].iterrows():
testNode = ET.SubElement(root, 'test-result', {
"exectime": f"{datetime_now}",
"result": "FAILURE",
"isdone": "TRUE")
testcaseNode = ET.SubElement(testNode, 'test-case', {
"testcasename": f"{row['testunit']}",
"testunit": f"{row['testname']}",
"testpath": f"{row['testpath']}"
})
ET.indent(root, space=" ")
tree_out = ET.tostring(root, pretty_print=True,
xml_declaration=True,
encoding="UTF-8")
print(tree_out)
print('writing into file --> output_lxml.xml')
with open('output_lxml.xml', 'wb') as file:
file.write(tree_out)
generateXML(df)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.