gpt4 book ai didi

python - 使用Python将XML解析为CSV nonetype错误

转载 作者:太空宇宙 更新时间:2023-11-03 14:00:52 24 4
gpt4 key购买 nike

我正在尝试将 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

最佳答案

我使用索引来访问有问题的子元素。有时,当您知道信息在哪里时,这会更容易做到。

您可以使用以下方法进行检查

for child in root[0]:
print(child.tag, child.attrib)

您可以通过继续索引来进一步导航 root[0][0][1] 等等

您必须记住,索引是父级,而您正在寻找子级。在您的情况下,根目录是 Nuix ,它将返回此实例中的子项 Export

root[0] 是“Export”,find 将搜索子项并返回您想要的内容,即 ExportConfiguration ,这里是您要查找的 CaseNameCaseLocation..

如果你这样做

for child in root[0][0]:
print(child.tag, child.attrib)

这将打印 CaseName 等标签,但您将无法在此级别使用 find 。您将在 CaseName 内搜索 CaseName

一旦您有了 parent ,您就可以更轻松地找到 child 。

此代码有效。

我已将空列表从循环中取出。

我还更改了 append 值,因为它们没有变量,只有字符串名称...我还缩进了一些附加内容,因为它们位于循环之外。

我留下了 print 语句,以便您可以看到发生了什么。

import xml.etree.ElementTree as ET
import csv

tree = ET.parse('summary-report.xml')
root = tree.getroot()

Resident_data = open('Parse_PythonOutput.csv', 'a')

# create the csv writer object

csvwriter = csv.writer(Resident_data)
resident_head = []
resident = []
address_list = []

count = 0
for member in root[0]:
if count == 0:

name = member.find('CaseName').tag
print(name)
resident_head.append(name)

location = member.find('CaseLocation').tag
print(location)
resident_head.append(location)

csvwriter.writerow(resident_head)
count = count + 1

name_text = member.find('CaseName').text
print(name_text)
resident.append(name_text)

text_location = member.find('CaseLocation').text
print(text_location)
resident.append(text_location)

print(resident)

csvwriter.writerow(resident)

Resident_data.close()

CSV 数据文件如下所示:

CaseName,CaseLocation
Brooklyn,C:\Users\KK132WQ\Desktop\Brooklyn Case - Nuix

关于python - 使用Python将XML解析为CSV nonetype错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49245474/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com