我正在尝试将XML文件解析为CSV。但是,我得到以下错误。 我已经用另一个简单的XML测试了逻辑,它似乎可以工作。我在下面提供了我的错误、XML文件、python代码和所需的输出。现在我只添加了两个专栏。我已经看了好几个小时了,如果你能再看一眼我会很感激的。谢谢您!你知道吗
错误:
name = member.find('CaseName').tag AttributeError: 'NoneType' object has no attribute 'tag'
XML文件:
<?xml version="1.0" encoding="UTF-8"?>
<Nuix version="7.2.2" architecture="amd64">
<Export
startTime="Sun Feb 25 22:07:07 2018 (America/Chicago)"
endTime="Sun Feb 25 22:08:03 2018 (America/Chicago)"
exportDuration="55s"
processingDuration="55s">
<ExportConfiguration>
<LoadFiles>
</LoadFiles>
<MessageFormat>NATIVE</MessageFormat>
<ExportDirectory>C:\Users\KK132WQ\Desktop\Brooklyn Case - Nuix\OCR cache directory</ExportDirectory>
<SeparateEmailAttachments>false</SeparateEmailAttachments>
<RegenerateNatives>false</RegenerateNatives>
<RegeneratePdfs>false</RegeneratePdfs>
<FindTopLevelItems>false</FindTopLevelItems>
<DescendantItems>false</DescendantItems>
<ExportContainers>false</ExportContainers>
<SortOrder>position</SortOrder>
<CaseName>Brooklyn</CaseName>
<CaseLocation>C:\Users\KK132WQ\Desktop\Brooklyn Case - Nuix</CaseLocation>
<TimeZone>America/Chicago</TimeZone>
<Numbering>
<Strategy>Document ID numbering</Strategy>
<DocumentPagesInSameFolder>true</DocumentPagesInSameFolder>
<FamilyDocumentsInSameFolder>false</FamilyDocumentsInSameFolder>
<FirstItemNumber>DOC-000000001</FirstItemNumber>
</Numbering>
<Imaging>
<ImagingProfile>Default</ImagingProfile>
</Imaging>
<Naming>
<NativeNamingScheme>Page only</NativeNamingScheme>
<PdfNamingScheme>Page only</PdfNamingScheme>
</Naming>
<OcrSettings>
<Recognition>High Quality - Slow</Recognition>
<Deskewed/>
<UpdateTextStore append="true"/>
<Rotation>Auto</Rotation>
<Languages>English</Languages>
</OcrSettings>
<ResemblanceThreshold>0.85</ResemblanceThreshold>
</ExportConfiguration>
<ExportStatistics>
<SelectedItems>4</SelectedItems>
<ExcludedCount>0</ExcludedCount>
<TotalItemsToExport>4</TotalItemsToExport>
<FailedItems>0</FailedItems>
<DocumentNumbers>
<First></First>
<Last></Last>
</DocumentNumbers>
</ExportStatistics>
<ExportStageDetails>
<Stage
name="WORK_QUEUE"
successfulItems="4"
failedItems="0"
duration="1s">
<SlipsheetItemDetails>
</SlipsheetItemDetails>
<FailedItemDetails>
</FailedItemDetails>
</Stage>
<Stage
name="NATIVE"
successfulItems="4"
failedItems="0"
duration="33s">
<SlipsheetItemDetails>
</SlipsheetItemDetails>
<FailedItemDetails>
</FailedItemDetails>
</Stage>
<Stage
name="STORED_EMAIL_FIXUP"
successfulItems="4"
failedItems="0"
duration="1s">
<SlipsheetItemDetails>
</SlipsheetItemDetails>
<FailedItemDetails>
</FailedItemDetails>
</Stage>
<Stage
name="PDF"
successfulItems="4"
failedItems="0"
duration="1s">
<SlipsheetItemDetails>
</SlipsheetItemDetails>
<FailedItemDetails>
</FailedItemDetails>
</Stage>
<Stage
name="BINARY_STORE"
successfulItems="0"
failedItems="0"
duration="0s">
<SlipsheetItemDetails>
</SlipsheetItemDetails>
<FailedItemDetails>
</FailedItemDetails>
</Stage>
<Stage
name="OCR_INITIALISATION"
successfulItems="4"
failedItems="0"
duration="0s">
<SlipsheetItemDetails>
</SlipsheetItemDetails>
<FailedItemDetails>
</FailedItemDetails>
</Stage>
<Stage
name="OCR"
successfulItems="4"
failedItems="0"
duration="17s">
<SlipsheetItemDetails>
</SlipsheetItemDetails>
<FailedItemDetails>
</FailedItemDetails>
</Stage>
<Stage
name="POST_OCR"
successfulItems="4"
failedItems="0"
duration="0s">
<SlipsheetItemDetails>
</SlipsheetItemDetails>
<FailedItemDetails>
</FailedItemDetails>
</Stage>
<Stage
name="TEXT_REPLACEMENT"
successfulItems="4"
failedItems="0"
duration="1s">
<SlipsheetItemDetails>
</SlipsheetItemDetails>
<FailedItemDetails>
</FailedItemDetails>
</Stage>
</ExportStageDetails>
<FileStatistics>
<NativeFilesExported>3</NativeFilesExported>
<NativeFilesFromStore>0</NativeFilesFromStore>
<NativeFilesExportedInline>0</NativeFilesExportedInline>
<NativeFilesExportedParallel>3</NativeFilesExportedParallel>
<NativeFilesExportedParallelLocal>0</NativeFilesExportedParallelLocal>
<NativeFilesWithInvalidTimes>0</NativeFilesWithInvalidTimes>
<NativePlaceHolderFilesExported>0</NativePlaceHolderFilesExported>
<NativeFilesRegenerated>0</NativeFilesRegenerated>
<TextFilesExported>0</TextFilesExported>
<TextPlaceHolderFilesExported>0</TextPlaceHolderFilesExported>
<PdfFilesExported>0</PdfFilesExported>
<PdfFilesStamped>0</PdfFilesStamped>
<TiffFilesExported>0</TiffFilesExported>
<PdfDetails>
<PdfFilesFromStore>0</PdfFilesFromStore>
<PdfFilesRegenerated>0</PdfFilesRegenerated>
<PdfFilesExportedInline>0</PdfFilesExportedInline>
<PdfFilesExportedParallel>0</PdfFilesExportedParallel>
<PdfFilesExportedParallelLocal>0</PdfFilesExportedParallelLocal>
<UserImportedPdfs>0</UserImportedPdfs>
<PrintedPdfs>0</PrintedPdfs>
<UnformattedTextPdfs>0</UnformattedTextPdfs>
<ItemEncryptedPdfs>0</ItemEncryptedPdfs>
<UnprintableItemPdfs>0</UnprintableItemPdfs>
</PdfDetails>
</FileStatistics>
<PageCountStatistics>
<PdfPages>0</PdfPages>
<StampedPages>0</StampedPages>
<FailedStampedPages>0</FailedStampedPages>
<AveragePageCount>0.0</AveragePageCount>
</PageCountStatistics>
<ThroughputStatistics>
<NativeDocRate>0.0857363321997085</NativeDocRate>
<PdfDocRate>0.0</PdfDocRate>
<StampedDocRate>0.0</StampedDocRate>
<PdfPageRate>0.0</PdfPageRate>
<StampingPageRate>0.0</StampingPageRate>
</ThroughputStatistics>
<MimeTypeStatistics>
<MimeTypes>
<MimeType name="application/pdf" count="4" />
</MimeTypes>
</MimeTypeStatistics>
</Export>
</Nuix>
Python代码:
import xml.etree.ElementTree as ET
import csv
tree = ET.parse('D:\\Users\\eferse\\Desktop\\XML_parsing\\summary-report.xml')
root = tree.getroot()
# open a file for writing
Resident_data = open('D:\\Users\\eferse\\Desktop\\XML_parsing\\Nuix Export XML Parse_PythonOutput.csv', 'w')
# create the csv writer object
csvwriter = csv.writer(Resident_data)
resident_head = []
count = 0
for member in root.findall('Export'):
resident = []
address_list = []
if count == 0:
name = member.find('CaseName').tag
resident_head.append(CaseName)
location= member.find('CaseLocation').tag
resident_head.append(CaseLocation)
csvwriter.writerow(resident_head)
count = count + 1
name = member.find('CaseName').text
resident.append(CaseName)
location= member.find('CaseLocation').text
resident.append(CaseLocation)
csvwriter.writerow(resident)
Resident_data.close()
所需输出:Output
我使用索引来访问相关的子元素。有时,当你知道信息在哪里时,这就更容易做到。你知道吗
您可以使用以下方法检查
你还可以继续索引,直到你喜欢的地方
你必须记住索引是父级的,你在寻找子级。在您的例子中,root是
Nuix
,它将返回这个实例中的子级Export
root[0]
是'Export',它find
将搜索子项并返回您想要的内容,即ExportConfiguration
,在这里是您要查找的内容CaseName
和CaseLocation
。。你知道吗如果你这样做了
这将打印
CaseName
等的标签,但您将无法在此级别使用find。您将在CaseName
内搜索CaseName
。你知道吗一旦你有了父母,你就能更容易找到孩子。你知道吗
这个代码有效。你知道吗
我已经把空名单从循环中去掉了。你知道吗
我还更改了
append
值,因为它们没有变量,只有字符串名。。。我还缩进了一些附录,因为它们在循环之外。你知道吗我把
print
语句留在了中,这样您就可以看到发生了什么。你知道吗CSV数据文件如下所示:
相关问题 更多 >
编程相关推荐