【发布时间】:2020-10-19 16:15:36
【问题描述】:
我有数百个 XML 文件需要转换为 XLSX 以进行下游分析,但我有太多的手动操作。我一直在尝试找出一种 python 方法来做到这一点,并找到了许多使用 xml2xlsx 或 xml.etree.ElementTree 的教程。但是,我遇到的问题是所有这些都在处理单张纸,而我的 xml 有 3 张纸(例如,请参见下面的部分)。如果有人知道如何做到这一点,或者我应该如何解析它,我将不胜感激。
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Worksheet ss:Name="Nodes">
<Table>
<Row>
<Cell><Data ss:Type="String">Node ID</Data></Cell>
<Cell><Data ss:Type="String">X Coord</Data></Cell>
<Cell><Data ss:Type="String">Y Coord</Data></Cell>
<Cell><Data ss:Type="String">Z Coord</Data></Cell>
<Cell><Data ss:Type="String">Coordination Number</Data></Cell>
</Row>
<Row>
<Cell><Data ss:Type="Number">0</Data></Cell>
<Cell><Data ss:Type="Number">12.25489235</Data></Cell>
<Cell><Data ss:Type="Number">21.835989</Data></Cell>
<Cell><Data ss:Type="Number">6.916931152</Data></Cell>
<Cell><Data ss:Type="Number">1</Data></Cell>
</Row>
...
</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
<Worksheet ss:Name="Points">
<Table>
<Row>
<Cell><Data ss:Type="String">Point ID</Data></Cell>
<Cell><Data ss:Type="String">thickness</Data></Cell>
<Cell><Data ss:Type="String">X Coord</Data></Cell>
<Cell><Data ss:Type="String">Y Coord</Data></Cell>
<Cell><Data ss:Type="String">Z Coord</Data></Cell>
</Row>
...
</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
<Worksheet ss:Name="Segments">
<Table>
<Row>
<Cell><Data ss:Type="String">Segment ID</Data></Cell>
<Cell><Data ss:Type="String">Node ID #1</Data></Cell>
<Cell><Data ss:Type="String">Node ID #2</Data></Cell>
<Cell><Data ss:Type="String">Point IDs</Data></Cell>
</Row>
...
这是我到目前为止所尝试的,首先使用 xml2xlsx,它返回一个空的 excel 表:
from xml2xlsx import xml2xlsx
from lxml import etree
import sys
import os
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-f', '--files', nargs = '+', required = True, help='one or more xml files to convert')
#parser.add_argument('-c', '--conditions', nargs = '+', default = ['Condition 1', 'Condition 2'], help = 'Names of conditions to analyze. replace any spaces in filename with underscores.')
args = parser.parse_args()
for file in args.files:
filename = os.path.splitext(os.path.basename(file))[0].lower()
filename = filename.replace(' ', '_')
output = filename+'.xlsx'
with open(file) as xml_file:
template = xml_file.read().encode()
with open(output, 'wb') as xlsx_file:
xlsx_file.write(xml2xlsx(template))
然后这个预先编写的代码,我无法开始工作。
#XML TO EXCEL FILE
import xml.etree.ElementTree as ET
from openpyxl import Workbook
import os
import sys
import argparse
def readFile(filename):
'''
Checks if file exists, parses the file and extracts the needed data
returns a 2 dimensional list without "header"
'''
if not os.path.exists(filename): return
tree = ET.parse(filename)
root = tree.getroot()
#you may need to adjust the keys based on your file structure
dict_keys = ["id","first_name","last_name","email","gender","ip_address" ] #all keys to be extracted from xml
mdlist = []
for child in root:
temp = []
for key in dict_keys:
temp.append(child.find(key).text)
mdlist.append(temp)
return mdlist
def to_Excel(mdlist):
'''
Generates excel file with given data
mdlist: 2 Dimenusional list containing data
'''
wb = Workbook()
ws = wb.active
for i,row in enumerate(mdlist):
for j,value in enumerate(row):
ws.cell(row=i+1, column=j+1).value = value
newfilename = os.path.abspath("./xml_to_excel.xlsx")
wb.save(newfilename)
print("complete")
return
parser = argparse.ArgumentParser()
parser.add_argument('-f', '--files', nargs = '+', required = True, help='one or more xml files to convert')
#parser.add_argument('-c', '--conditions', nargs = '+', default = ['Condition 1', 'Condition 2'], help = 'Names of conditions to analyze. replace any spaces in filename with underscores.')
args = parser.parse_args()
for f in args.files:
result = readFile(f)
if result:
to_Excel(result)
最后,这个,它吐出了一长串无意义的标签。
import xml.etree.ElementTree as ET
tree = ET.parse(r'pathToXML')
root = tree.getroot()
tag = root.tag
att = root.attrib
#Flatten XML to CSV
for child in root:
for subchild in child:
mainlevel = child.tag
xmltocsv = ''
for elem in root.iter():
if elem.tag == root.tag:
continue
if elem.tag == mainlevel:
xmltocsv = xmltocsv + '\n'
xmltocsv = xmltocsv + str(elem.tag).rstrip() + str(elem.attrib).rstrip() + ';' + str(elem.text).rstrip() + ';'
with open('output.csv', 'w') as file:
file.write(xmltocsv)
【问题讨论】:
-
这不是代码编写服务。如果您需要帮助,您至少应该诚实地尝试解决方案。
-
我添加了到目前为止我尝试过的内容。主要问题是我对 XML 文件非常不熟悉,所以我认为展示我迄今为止所做的一切只会比获得公正的帮助更令人困惑。
-
你能用MS Excel这个应用打开
.xml这样的文件吗? -
是的,我最终只是通过使用 excel 打开它们并保存为 .xlsx 来手动转换它们。我希望将其自动化,因为有 270 个文件,但最终手动完成比尝试以这种方式计算要快。