我正在尝试使用某些键名称和值修改构建python dict项目并整合所有词典。
#!/usr/bin/python
def Cftemplatebuild():
templateBucketName = {
"NewBucketName" : {
"Description" : "Bucket name",
"Type" : "String",
"Default": "bucketname",
"AllowedPattern" : "[-a-z0-9\\-]*",
"ConstraintDescription" : " Must be lowercase no spaces."
}
}
i = 1
TemplateBucketName = {}
TemplateBucketPolicy = {}
bucketNameWithLink = [('TEST','TEST-LINK'),('PROD','PROD-LINK')]
for items in bucketNameWithLink:
print "ITEMS"
print items[0]
print items[1]
print "BUCKET NAME CREATION"
print 'actual', templateBucketName
templateBucketNamecpy = templateBucketName.copy()
print 'copy',templateBucketNamecpy
templateBucketNamecpy['NewBucketName'+str(i)] =
templateBucketNamecpy.pop('NewBucketName')
print 'Modified key name', templateBucketNamecpy
templateBucketNamecpy['NewBucketName'+str(i)]['Default'] = items[0]
templateBucketNamecpy1 = templateBucketNamecpy.copy()
print 'Modfied key value',templateBucketNamecpy
print 'Copy Modified key value',templateBucketNamecpy1
print "templateBucketName-1"
print TemplateBucketName
TemplateBucketName.update(templateBucketNamecpy1)
print TemplateBucketName
#templateBucketName['NewBucketName'] =
templateBucketName.pop('NewBucketName'+str(i))
print "templateBucketName-revert"
#print TemplateBucketName
print 'EnD actual',templateBucketName
print "BUCKET NAME CREATION ENDS"
i= i + 1
if __name__ == "__main__":
Cftemplatebuild()
预期产出:
{'NewBucketName1':{'Default':'TEST','AllowedPattern':'[ - a-z0-9 \ - ] ','Type':'String','Description':'存储桶名称','ConstraintDescription':'必须小写无空格。'},'NewBucketName2':{'默认':'PROD','AllowedPattern':'[ - a-z0-9 \ - ] ','Type':'String','Description':'Bucket name','ConstraintDescription':'必须小写无空格。'}}
获得的实际产出:
{'NewBucketName1':{'Default':'PROD','AllowedPattern':'[ - a-z0-9 \ - ] ','Type':'String','Description':'存储桶名称','ConstraintDescription':'必须小写无空格。'},'NewBucketName2':{'默认':'PROD','AllowedPattern':'[ - a-z0-9 \ - ] ','Type':'String','Description':'Bucket name','ConstraintDescription':'必须小写无空格。'}}