在这里我添加了一个代码来生成 xml_tags,这就是我的输出:
md5 value = "sdfhsdkjgfjw35378563"
repOperation value = "push"
size value = "toolarge"
images value = "/home/rakesh/from_t_jack/imag1.ipds"
status value = "completed"
replication value = "mode"
sdlist value = "f:"
我用过字典。我的字典不能正确sort
。我希望字典按照我声明的顺序进行排序和打印。另一个问题是,如果在第 6 行观察到输出,则输出有点反转,我期待:mode value = "replication"
。但是输出是相反的。我不知道它为什么会这样。我想知道如何解决这两个问题。这是我的代码。
def generate_tag(name,val):
full_tag = ''+name+' value = "'+val+'"'
return full_tag
def auto_xml(sdList,repOperation,images,status,md5):
tags = {'repOperationTag' :{'repOperation_tag_name':'repOperation' ,
'repOperation_val' : repOperation },
'modeTag' :{'mode_tag_name':'mode' ,
'mode_val' : 'replication' },
'imagesTag' :{'images_tag_name': 'images',
'images_val' : images },
'statusTag' :{'status_tag_name':'status' ,
'status_val' : status},
'sizeTag' :{'size_tag_name':'size' ,
'size_val' : 'toolarge'},
'md5Tag' :{'md5_tag_name' : 'md5' ,
'md5_val' : md5} ,
'sdListTag' :{'sdList_tag_name': 'sdlist' ,
'sdList_val' : sdList} }
count = 0
tag_len = len(tags)
while count < tag_len :
inner = tags[tags.keys()[count]]
tag_name = inner[inner.keys()[0]]
tag_val = inner[inner.keys()[1]]
full_tag = generate_tag(tag_name,tag_val)
print full_tag
count = count + 1
def myordered_dict():
from collections import OrderedDict
tags = {'A_repOperationTag' :{'tag_name':'repOperation' ,
'tag_val' : repOperation },
'B_modeTag' :{'tag_name':'mode' ,
'tag_val' : 'replication' },
'C_imagesTag' :{'tag_name': 'images',
'tag_val' : images },
'D_statusTag' :{'tag_name':'status' ,
'tag_val' : status},
'E_sizeTag' :{'tag_name':'size' ,
'tag_val' : 'toolarge'},
'F_md5Tag' :{'tag_name' : 'md5' ,
'tag_val' : md5} ,
'G_sdListTag' :{'tag_name': 'sdlist' ,
'tag_val' : sdList},
'H_machinename' :{'tag_name': 'hostname' ,
'tag_val' : 'xp_vm'} }
tags = OrderedDict(sorted(tags.items(), key=lambda t: t[0]))