如何从2个XML文件读取并将数据导出到Excel文件?

时间:2019-04-13 11:45:24

标签: xml python-3.x

我有2个XML文件,其中包含一些数据,我尝试将此数据导出到Excel文件。 我设法从1个文件(ItemType.xml)导出数据,但问题出在第二个文件(ItemTip.xml)。 第二个文件中的数据标签与第一个文件中的数据标签有很大关系,所以我需要的是,在第二个文件的数据标签上运行以获取编号,然后在第一个文件中找到它,然后在可以存储的数量较多时使用相关行中的数据。

XML文件: https://ufile.io/yp53flm8

我们在(ItemType.xml)<< strong>数据Data_14 =“ 15049” StringRef =“ String_194” /> 中, 在文件中以及下面的描述: << strong> String_194 Data_0 =“ Stamp Hammer(R)” />

在第二个文件(ItemTip.xml)中,我们有:<< strong> Data Data_0 =“ 15049” StringRef =“ String_0” /> ,在文件中,我们的描述是: << strong>字符串_0 Data_0 =“用于获取| 400%资源的挖掘工具” Data_1 =“提高成功率|以获取高级资源” />

所以我的目标是制作一个具有下一个数据的excel文件:

ReferenceID |商品名称| ItemDescription_1

15049,锻锤(R),采矿工具以获得| 400%的资源

import xlsxwriter
from xml.dom import minidom
# import json

# create a new xlsx file.
workbook = xlsxwriter.Workbook('Item Data.xlsx')
worksheet = workbook.add_worksheet()
bold = workbook.add_format({'bold': True})
worksheet.set_column('A:H', 25)
worksheet.write('A1', 'VisualID', bold)
worksheet.write('B1', 'bType', bold)
worksheet.write('C1', 'bKind', bold)
worksheet.write('D1', 'ReqClass', bold)
worksheet.write('E1', 'ReferenceID', bold)
worksheet.write('F1', 'ItemName', bold)
worksheet.write('G1', 'ItemDescription_1', bold)
worksheet.write('H1', 'ItemDescription_2', bold)

row = 1
colA = 0
colB = 1
colC = 2
colD = 3
colE = 4
colF = 5
colG = 6
colH = 7


# parse an xml file by name
mydoc = minidom.parse('ItemType.xml')
allItems = mydoc.getElementsByTagName('Data')

mydoc2 = minidom.parse('ItemTip.xml')
allItems2 = mydoc2.getElementsByTagName('Data')

# all item attributes
for elem in allItems:
    VisualID = int(elem.attributes['Data_0'].value)
    worksheet.write_number(row, colA, VisualID)  # add the VisualID of the Item
    # print(VisualID)
    bType = int(elem.attributes['Data_4'].value)
    worksheet.write_number(row, colB, bType)  # add the bType of the Item
    # print(bType)
    bKind = int(elem.attributes['Data_5'].value)
    worksheet.write_number(row, colC, bKind)  # add the bKind of the Item
    # print(bKind)
    ReqClass = int(elem.attributes['Data_13'].value)
    worksheet.write_number(row, colD, ReqClass)  # add the ReqClass of the Item
    # print(ReqClass)
    ReferenceID = int(elem.attributes['Data_14'].value)
    worksheet.write_number(row, colE, ReferenceID)  # add the ReferenceID of the Item
    # print(ReferenceID)
    stringNames = elem.attributes['StringRef'].value
    stringName = mydoc.getElementsByTagName(stringNames)
    Name = stringName[1].attributes['Data_0'].value
    print(Name)
    worksheet.write(row, colF, Name)  # add the Name of the Item
    row += 1

workbook.close()

0 个答案:

没有答案