gpt4 book ai didi

python - 属性错误: 'NodeList' object has no attribute 'getElementsByTagName'

转载 作者:太空宇宙 更新时间:2023-11-03 16:49:32 25 4
gpt4 key购买 nike

我正在尝试从 xml 文件中提取文本内容。 xml文件是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<brca:tcga_bcr xsi:schemaLocation="http://tcga.nci/bcr/xml/clinical/brca/2.7 http://tcga-data.nci.nih.gov/docs/xsd/BCR/tcga.nci/bcr/xml/clinical/brca/2.7/TCGA_BCR.BRCA_Clinical.xsd" schemaVersion="2.7" xmlns:brca="http://tcga.nci/bcr/xml/clinical/brca/2.7" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:admin="http://tcga.nci/bcr/xml/administration/2.7" xmlns:clin_shared="http://tcga.nci/bcr/xml/clinical/shared/2.7" xmlns:shared="http://tcga.nci/bcr/xml/shared/2.7" xmlns:brca_shared="http://tcga.nci/bcr/xml/clinical/brca/shared/2.7" xmlns:shared_stage="http://tcga.nci/bcr/xml/clinical/shared/stage/2.7" xmlns:brca_nte="http://tcga.nci/bcr/xml/clinical/brca/shared/new_tumor_event/2.7/1.0" xmlns:nte="http://tcga.nci/bcr/xml/clinical/shared/new_tumor_event/2.7" xmlns:rx="http://tcga.nci/bcr/xml/clinical/pharmaceutical/2.7" xmlns:rad="http://tcga.nci/bcr/xml/clinical/radiation/2.7">
<admin:admin>
<admin:bcr xsd_ver="1.17">Nationwide Children's Hospital</admin:bcr>
<admin:file_uuid xsd_ver="2.6">6CEF6ECD-264E-4DF6-8419-9E4C564DA7B2</admin:file_uuid>
<admin:batch_number xsd_ver="1.17">85.84.0</admin:batch_number>
<admin:project_code xsd_ver="">TCGA</admin:project_code>
<admin:disease_code xsd_ver="2.6">BRCA</admin:disease_code>
<admin:day_of_dcc_upload xsd_ver="1.17">21</admin:day_of_dcc_upload>
<admin:month_of_dcc_upload xsd_ver="1.17">1</admin:month_of_dcc_upload>
<admin:year_of_dcc_upload xsd_ver="1.17">2016</admin:year_of_dcc_upload>
<admin:patient_withdrawal>
<admin:withdrawn>false</admin:withdrawn>
</admin:patient_withdrawal>
</admin:admin>

我收到以下错误:

AttributeError: 'NodeList' object has no attribute 'getElementsByTagName'

我使用的是Python 2.7。这是我的代码的一部分。我不知道出了什么问题。有什么建议吗?

from xml.dom import minidom
xmldoc = minidom.parse('A0SD.xml')
bcr = xmldoc.getElementsByTagNameNS('*','tcga_bcr')
patient_info = bcr.getElementsByTagName('admin')

最佳答案

getElementsByTagNameNS 返回具有指定标记的所有节点的列表。所以bcr是一个NodeList。您不能请求 NodeListgetElemnetsByTagName,而只能请求 NodegetElemnetsByTagName。您将需要迭代 bcr 以获取每个节点中的 admin 标记。或者,如果您希望只有一个 tcga_bcr 标记,那么您可以只获取列表的第一个元素:

bcr = xmldoc.getElementsByTagNameNS('*','tcga_bcr')[0]

关于python - 属性错误: 'NodeList' object has no attribute 'getElementsByTagName' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35998161/

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