ElementTree不打印属性

2024-04-28 20:58:43 发布

您现在位置:Python中文网/ 问答频道 /正文

您好,我有下一个xml文件:

<?xml version="1.0"?>
<!-- MPD file Generated with GPAC version 0.7.2-DEV-rev986-gb3353c225-master  at 2020-04-05T02:33:59.948Z-->
<MPD xmlns="urn:mpeg:dash:schema:mpd:2011" minBufferTime="PT1.500S" type="static" mediaPresentationDuration="PT0H3M0.000S" maxSubsegmentDuration="PT0H0M7.000S" profiles="urn:mpeg:dash:profile:isoff-on-demand:2011">
 <ProgramInformation moreInformationURL="http://gpac.io">
  <Title>video.mpd generated by GPAC</Title>
 </ProgramInformation>

 <Period duration="PT0H3M0.000S">
  <AdaptationSet segmentAlignment="true" maxWidth="2592" maxHeight="1080" maxFrameRate="24" par="2592:1080" lang="eng" startWithSAP="1" subsegmentAlignment="true" subsegmentStartsWithSAP="1">
   <Representation id="1" mimeType="video/mp4" codecs="avc1.640032" width="2592" height="1080" frameRate="24" sar="1:1" bandwidth="1729262">
    <BaseURL>video-1080_dashinit.mp4</BaseURL>
    <SegmentBase indexRangeExact="true" indexRange="920-1263">
     <Initialization range="0-919"/>
    </SegmentBase>
   </Representation>
   <Representation id="2" mimeType="video/mp4" codecs="avc1.640020" width="1728" height="720" frameRate="24" sar="1:1" bandwidth="862289">
    <BaseURL>video-720_dashinit.mp4</BaseURL>
    <SegmentBase indexRangeExact="true" indexRange="919-1262">
     <Initialization range="0-918"/>
    </SegmentBase>
   </Representation>
   <Representation id="3" mimeType="video/mp4" codecs="avc1.64001E" width="864" height="360" frameRate="24" sar="1:1" bandwidth="444517">
    <BaseURL>video-360_dashinit.mp4</BaseURL>
    <SegmentBase indexRangeExact="true" indexRange="919-1262">
     <Initialization range="0-918"/>
    </SegmentBase>
   </Representation>
  </AdaptationSet>
  <AdaptationSet segmentAlignment="true" lang="eng" startWithSAP="1" subsegmentAlignment="true" subsegmentStartsWithSAP="1">
   <Representation id="4" mimeType="audio/mp4" codecs="mp4a.40.2" audioSamplingRate="44100" bandwidth="129580">
    <AudioChannelConfiguration schemeIdUri="urn:mpeg:dash:23003:3:audio_channel_configuration:2011" value="2"/>
    <BaseURL>video-audio_dashinit.mp4</BaseURL>
    <SegmentBase indexRangeExact="true" indexRange="863-1338">
     <Initialization range="0-862"/>
    </SegmentBase>
   </Representation>
  </AdaptationSet>
 </Period>
</MPD>

我试图从Python中的标记中获取属性segmentAlignment的值,如下所示:

tree = ET.parse(MPDPath)
root = tree.getroot()

Periods = root.findall('Period')

for Period in Periods:

    AdaptationSets = Period.findall('AdaptationSet')

    for AdaptationSet in AdaptationSets:

        print(AdaptationSet.attrib['segmentAlignment'])

但是它总是返回None或者没有从该属性中提取字符串

我做错了什么?我知道MPD路径是正确的


Tags: idtruevideompdperiodmp4mimetypecodecs
1条回答
网友
1楼 · 发布于 2024-04-28 20:58:43

您的代码应该使用名称空间来查找此XML,如下所示:

ns = {'url': 'urn:mpeg:dash:schema:mpd:2011'}

for period in root.findall('url:Period', ns):
    adaptationSets = period.findall('url:AdaptationSet', ns)
    for adaptset in adaptationSets:
      print(adaptset.attrib['segmentAlignment'])

以下是文档:https://docs.python.org/3/library/xml.etree.elementtree.html#parsing-xml-with-namespaces

相关问题 更多 >