문제

Apple의 .Plist 형식 인 XML에서 객체를 읽고 직렬화해야합니다. 파이썬에서 가장 지능적인 방법은 무엇입니까? 기성품 솔루션이 있습니까?

도움이 되었습니까?

해결책

체크 아웃 plistlib.

다른 팁

Mac에 있다고 가정하면 pyobjc를 사용할 수 있습니다.

다음은 Plist의 독서의 예입니다. 시스템 관리를 위해 Python 사용, 슬라이드 27.

from Cocoa import NSDictionary

myfile = "/Library/Preferences/com.apple.SoftwareUpdate.plist"
mydict = NSDictionary.dictionaryWithContentsOfFile_(myfile)

print mydict["LastSuccessfulDate"]

# returns: 2009-08-11 08:38:01 -0600

그리고 Plist에게 글을 쓰는 예 (내가 쓴) :

#!/usr/bin/env python

from Cocoa import NSDictionary, NSString

myfile = "~/test.plist"
myfile = NSString.stringByExpandingTildeInPath(myfile)

mydict = {"Nice Number" : 47, "Universal Sum" : 42}
mydict["Vector"] = (10, 20, 30)
mydict["Complex"] = [47, "i^2"]
mydict["Truth"] = True

NSDictionary.dictionaryWithDictionary_(mydict).writeToFile_atomically_(myfile, True)

내가 달릴 때 defaults read ~/test Bash에서는 다음과 같습니다.

{
    Complex =     (
        47,
        "i^2"
    );
    "Nice Number" = 47;
    Truth = 1;
    "Universal Sum" = 42;
    Vector =     (
        10,
        20,
        30
    );
}

그리고 Property List Editor.App에서 열 때 파일이 매우 멋지게 보입니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top