Python - 从.plist中提取

时间:2017-05-20 12:26:57

标签: python python-2.7 python-3.x

我得到了这段代码,一个.plist。 有没有办法提取它的某些部分? 我只想要字符串,如果可能的话,只需要一些特定的字符串。

就像我可以提取-(bool) isAgent一样 知道<key>displayName</key>位于其上方,<key>prefix</key>位于其下方。

                <string>ZDKUser</string>
                <key>displayName</key>
                <string>-(bool) isAgent</string>
                <key>prefix</key>

在这个区块之外^

这可能吗?我该怎么办? 如果用python不可能,怎么回事?

    <array>
        <dict>
            <key>UUID</key>
            <string>123-456-789-0123</string>
            <key>purchaseNumber</key>
            <string>y.number.x</string>
            <key>purchaseID</key>
            <string>3.5.6</string>
            <key>name</key>
            <string>*Product Name*</string>
            <key>purchased</key>
            <false/>
            <key>units</key>
            <array>
                <dict>
                    <key>methodObjc</key>
                    <dict>
                        <key>className</key>
                        <string>ZDKUser</string>
                        <key>displayName</key>
                        <string>-(bool) isAgent</string>
                        <key>prefix</key>
                        <string>-</string>
                        <key>selector</key>
                        <string>isAgent</string>
                        <key>typeEncoding</key>
                        <string>B16@0:8</string>
                    </dict>
                    <key>name</key>
                    <string>Unit for -(bool) isAgent</string>
                    <key>overrides</key>
                    <array>
                        <dict>
                            <key>argument</key>
                            <integer>0</integer>
                            <key>type</key>
                            <dict>
                                <key>subtype</key>
                                <integer>0</integer>
                                <key>type</key>
                                <integer>6</integer>
                            </dict>
                            <key>value</key>
                            <dict>
                                <key>type</key>
                                <integer>6</integer>
                                <key>value</key>
                                <true/>
                            </dict>
                        </dict>
                    </array>
                </dict>
                <dict>
                    <key>methodObjc</key>
                    <dict>
                        <key>className</key>
                        <string>AFUserAccount</string>
                        <key>displayName</key>
                        <string>-(void) setAuto_renew:(bool)</string>
                        <key>prefix</key>
                        <string>-</string>
                        <key>selector</key>
                        <string>setAuto_renew:</string>
                        <key>typeEncoding</key>
                        <string>v20@0:8B16</string>
                    </dict>
                    <key>name</key>
                    <string>Unit for -(void) setAuto_renew:(bool)</string>
                    <key>overrides</key>
                    <array>
                        <dict>
                            <key>argument</key>
                            <integer>1</integer>
                            <key>type</key>
                            <dict>
                                <key>subtype</key>
                                <integer>0</integer>
                                <key>type</key>
                                <integer>6</integer>
                            </dict>
                            <key>value</key>
                            <dict>
                                <key>type</key>
                                <integer>6</integer>
                                <key>value</key>
                                <true/>
                            </dict>
                        </dict>
                    </array>
                </dict>
<array/>
</dict>
</plist>

1 个答案:

答案 0 :(得分:0)

import lxml.etree as etree
import sys

doc = etree.parse(open(sys.argv[1]))
for app_dict in doc.xpath('/plist/dict/array/dict'):
    appId = app_dict.xpath('./key[.="appIdentifier"]/following-sibling::string[1]/text()')[0]
    for method_dict in app_dict.xpath('.//dict[key="methodObjc"]/dict'):
        classId = method_dict.xpath('./key[.="className"]/following-sibling::string[1]/text()')[0]
        methodId = method_dict.xpath('./key[.="displayName"]/following-sibling::string[1]/text()')[0] 
        print 'App: %s; Class: %s; Method: %s' % (appId, classId, methodId)

回答@Charles Duffy