gpt4 book ai didi

python - 在 Python 中导入 XML 字典

转载 作者:太空宇宙 更新时间:2023-11-03 19:20:07 25 4
gpt4 key购买 nike

我需要一些 python 方面的帮助。我是Python新手。我曾经用 Objective-C 进行过很多编程。

在 Objective-C 中,我能够将 NSDictionary 保存为 xml 文件,并使用简单的命令直接加载它。这是我保存的 xml 字典的示例:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>1</key>
<string>Object One</string>
<key>2</key>
<string>Object Two</string>
<key>3</key>
<string>Object Three</string>
</dict>
</plist>
<小时/>

如果我在 Objective-C 中加载它,我可以直接获取 NSDictionary 形式的字典。

所以我的问题是:如何在 Python 中轻松做到这一点。我发现了一些使用解析器(例如 lxml)的东西。我设法加载数据,但无法获得仅包含键和值的字典。我在互联网上搜索了很多,但找不到合适的东西。有人有想法或建议吗?

我试过这个:http://code.activestate.com/recipes/410469-xml-as-dictionary/但我没能成功。

提前,非常感谢!

最佳答案

使用pliSTLib:http://docs.python.org/dev/library/plistlib.html

pl = readPlist(pathOrFile)
print(pl["1"])

关于python - 在 Python 中导入 XML 字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10025752/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com