2012-01-13 156 views
3

我目前正在使用Plistlib模塊來讀取Plist文件,但是當它涉及到二進制Plist文件時,我目前遇到了問題。用Python讀取二進制Plist文件

我想讀取數據到一個字符串,以便以後分析/打印等。我想知道如果他們無論如何讀取二進制Plist文件中不使用plutil函數並將二進制文件轉換爲XML?

謝謝您的幫助和時間提前。

+1

[二進制的plist Python模塊]的可能的複製(http://stackoverflow.com/q/3725268/222914) – 2012-01-13 20:13:38

+0

謝謝Janne,我看過這篇文章,但我希望如果有人知道如何在不安裝單獨模塊的情況下執行上述操作並僅使用預安裝的模塊Python提供。 – 2012-01-14 09:29:53

+0

名爲plistlib的默認模塊不處理二進制plist文件。你最好選擇「biplist」或其他第三模塊。 – 2012-06-18 07:46:16

回答

4

雖然你沒有指定plutil,因爲它預裝Mac上用它工作的解決方案可能是有用的人:

import json 
from subprocess import Popen, PIPE 

def plist_to_dictionary(filename): 
    "Pipe the binary plist through plutil and parse the JSON output" 
    with open(filename, "rb") as f: 
     content = f.read() 
    args = ["plutil", "-convert", "json", "-o", "-", "--", "-"] 
    p = Popen(args, stdin=PIPE, stdout=PIPE) 
    out, err = p.communicate(content) 
    return json.loads(out) 

print plist_to_dictionary(path_to_plist_file) 
+1

如果給這個函數提供了足夠大的輸入,它會停留在p.stdin.write(內容)上。請閱讀文檔。 – zhangyoufu 2016-03-03 04:30:01

+0

@張有福正確,謝謝 – orip 2016-03-03 09:52:08

0

你可以看看CFBinaryPList.c源文件,看看它是如何在C.

基於文件的實施,它的格式是這樣:

HEADER 
    magic number ("bplist") 
    file format version (currently "0?") 

OBJECT TABLE 
    variable-sized objects 

    Object Formats (marker byte followed by additional info in some cases) 
    null 0000 0000   // null object [v"1?"+ only] 
    bool 0000 1000   // false 
    bool 0000 1001   // true 
    url 0000 1100 string  // URL with no base URL, recursive encoding of URL string [v"1?"+ only] 
    url 0000 1101 base string // URL with base URL, recursive encoding of base URL, then recursive encoding of URL string [v"1?"+ only] 
    uuid 0000 1110   // 16-byte UUID [v"1?"+ only] 
    fill 0000 1111   // fill byte 
    int 0001 0nnn ...  // # of bytes is 2^nnn, big-endian bytes 
    real 0010 0nnn ...  // # of bytes is 2^nnn, big-endian bytes 
    date 0011 0011 ...  // 8 byte float follows, big-endian bytes 
    data 0100 nnnn [int] ... // nnnn is number of bytes unless 1111 then int count follows, followed by bytes 
    string 0101 nnnn [int] ... // ASCII string, nnnn is # of chars, else 1111 then int count, then bytes 
    string 0110 nnnn [int] ... // Unicode string, nnnn is # of chars, else 1111 then int count, then big-endian 2-byte uint16_t 
    string 0111 nnnn [int] ... // UTF8 string, nnnn is # of chars, else 1111 then int count, then bytes [v"1?"+ only] 
    uid 1000 nnnn ...  // nnnn+1 is # of bytes 
     1001 xxxx   // unused 
    array 1010 nnnn [int] objref* // nnnn is count, unless '1111', then int count follows 
    ordset 1011 nnnn [int] objref* // nnnn is count, unless '1111', then int count follows [v"1?"+ only] 
    set 1100 nnnn [int] objref* // nnnn is count, unless '1111', then int count follows [v"1?"+ only] 
    dict 1101 nnnn [int] keyref* objref* // nnnn is count, unless '1111', then int count follows 
     1110 xxxx   // unused 
     1111 xxxx   // unused 

OFFSET TABLE 
    list of ints, byte size of which is given in trailer 
    -- these are the byte offsets into the file 
    -- number of these is in the trailer 

TRAILER 
    byte size of offset ints in offset table 
    byte size of object refs in arrays and dicts 
    number of offsets in offset table (also is number of objects) 
    element # in offset table which is top level object 
    offset table offset