Pregunta

Necesito leer y serializar objetos desde y hacia XML, el formato .Plist de Apple en particular. ¿Cuál es la manera más inteligente de hacerlo en Python? ¿Hay soluciones ya hechas?

¿Fue útil?

Solución

Otros consejos

Si se asume que usted está en un Mac, puede utilizar PyObjC.

Aquí es un ejemplo de la lectura de un plist, desde Usando Python Para el Sistema administración , deslice 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

Y un ejemplo de escritura en un plist (que escribí):

#!/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)

Cuando a continuación, ejecutar defaults read ~/test en bash, me sale:

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

Y el archivo se ve muy bien cuando se abre en Lista de propiedades Editor.app.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top