【问题标题】:tracing an XML trace file and find index using python ET. trace使用 python ET 跟踪 XML 跟踪文件并查找索引。痕迹
【发布时间】:2022-01-06 07:27:41
【问题描述】:

我有一个 XML 跟踪文件,它有不同的标签。我正在尝试在文件中查找 XY 值来计算运动矢量。我将部分文件放在下面:

<Picture id="1" poc="1">
    <GOPNr>0</GOPNr>
    <SubPicture structure="0">
        <Slice num="0">
            <Type>0</Type>
            <TypeString>SLICE_TYPE_P</TypeString>
            <NAL>
                <Num>6</Num>
                <Type>1</Type>
                <TypeString>NALU_TYPE_SLICE</TypeString>
                <Length>178927</Length>
            </NAL>
            <MacroBlock num="0">
                <Position>
                    <X>0</X>
                    <Y>0</Y>
                </Position>
                <QP_Y>23</QP_Y>
                <Type>5</Type>
                <TypeString>I_8x8</TypeString>
                <PredModeString>BLOCK_TYPE_I</PredModeString>
                <SkipFlag>0</SkipFlag>
            </MacroBlock>

这个文件有很多MacroBlock 标签,但其中一些标签有MotionVector。为了提取 XY 值,我使用了以下代码:

abs_x_tag=list(qpy_node.text for qpy_node in root.findall('Picture/SubPicture/Slice/MacroBlock/SubMacroBlock/MotionVector/Absolute/X'))
       

我需要找到与特定TypeString 相关的XY 值,但有一次我只能使用上面的代码提取一个值。例如,我需要找到与&lt;TypeString&gt;P 相关的 X 和 Y 值,但我不知道该怎么做。你能帮我解决这个问题吗? 谢谢

【问题讨论】:

  • 我需要找出这些 x 和 y 值在哪个图片和宏块中,因为我需要只为 TypeStiring 为 P 的宏块计算运动矢量,对吧?
  • abs_x_tag 包含 XML 文件中的所有 X 值,但我需要找到宏块中 PredModeString 为 Block_Type_P 或 Blcok_TYpe_B 的 X 值?
  • 我需要根据它们的块类型来分隔这些 X 值。
  • 这个新代码中没有绝对/运动矢量

标签: python python-3.x xml-parsing


【解决方案1】:

当您想要解析一个块中的多个值时,特别是如果您必须根据其中一个或多个值做出决定,请逐块解析。如果您分别提取值,则会失去它们之间的链接

data="""<Picture><MacroBlock num="936">
                    <MotionVector list="0">
                        <Absolute>
                            <X>0</X>
                            <Y>0</Y>
                        </Absolute>
                    </MotionVector>
                    <TypeString>P_L0_16x16</TypeString>
                </MacroBlock>
                <MacroBlock num="937">
                    <MotionVector list="0">
                        <Absolute>
                            <X>1</X>
                            <Y>2</Y>
                        </Absolute>
                    </MotionVector>
                    <TypeString>B_L0_16x16</TypeString>
                </MacroBlock></Picture>"""
                
import xml.etree.ElementTree as ET
root=ET.fromstring(data)
blocks=root.findall('MacroBlock')
for block in blocks:
    x=block.find('MotionVector/Absolute/X').text
    y=block.find('MotionVector/Absolute/Y').text
    type_string=block.find('TypeString').text[0]
    print(x, y, type_string)
>>> 0 0 P
>>> 1 2 B

【讨论】:

  • 我使用了你的代码,但它产生了这个错误“AttributeError: 'NoneType' object has no attribute 'text'”,我不知道是什么问题。
  • 我认为这是因为您的真实代码和您的 xml 示例之间存在差异。我在您的代码中看到一些 SubMacroBlock 在您的 xml 中称为 MacroBlock
  • 我没有 SubMacroBlock 元素。我拥有的所有元素都与我上面的例子一样。所以我无法理解你的评论?
  • 在你的代码中你有这个'Picture/SubPicture/Slice/MacroBlock/SubMacroBlock/MotionVector/Absolute/X'
  • 对不起,我有。有些部分没有 SubMacroBlock,有些部分有。所以这意味着有了 SubMacroBlock 我找不到我想要的东西并且没有解决方案?
猜你喜欢
  • 1970-01-01
  • 2011-02-09
  • 2017-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-19
  • 1970-01-01
相关资源
最近更新 更多