Python plistlib:没有正确嵌套?
我的代码:Python plistlib:没有正确嵌套?,python,list,dictionary,plist,Python,List,Dictionary,Plist,我的代码: current_bex = dict( objectName = 'myData', objects = list( dict( one = 1, foo = 'bar', ), ), ) try: writePlist(current_bex, 'someFile.plist') except TypeError: print 'caught t
current_bex = dict(
objectName = 'myData',
objects = list(
dict(
one = 1,
foo = 'bar',
),
), )
try:
writePlist(current_bex, 'someFile.plist') except TypeError:
print 'caught typeerror'
结果:
<?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>objectName</key>
<string>myData</string>
<key>objects</key>
<array>
<string>foo</string>
<string>one</string>
</array>
</dict>
</plist>
很好。显然我对dict()和list()的使用是不正确的?有人能解释一下吗?这里有两件事:
list()
构造函数不接受单个内容项作为参数,而是接受项的iterable。因此,您可以编写列表文字语法[1,2,3]
,但不能编写list(1,2,3)
>>> list(dict(one=1, foo='bar'))
['foo', 'one']
>>> list(dict(one=1, foo='bar'))
['foo', 'one']