不使用内置函数将python字典转换为xml字符串

不使用内置函数将python字典转换为xml字符串,python,string,dictionary,Python,String,Dictionary,我有一个带有值(字符串、列表、dict)的字典,我想将该字典转换为xml格式的字符串 包含的值可能是子字典和列表(不是固定格式)。因此,我希望从dict中获取所有值并形成xml字符串,而不使用任何内置函数,如导入xml、ElementTree等 例如: 输入: {'Employee':{ 'Id' : 'TA23434', 'Name':'Kesavan' , 'Email':'k7@gmail.com' , 'Roles':[ {'Name':'Admin' ,'RoleId':'xa1234

我有一个带有值(字符串、列表、dict)的字典,我想将该字典转换为xml格式的字符串

包含的值可能是子字典和列表(不是固定格式)。因此,我希望从dict中获取所有值并形成xml字符串,而不使用任何内置函数,如导入xml、ElementTree等

例如:

输入:

{'Employee':{ 'Id' : 'TA23434', 'Name':'Kesavan' , 'Email':'k7@gmail.com' , 'Roles':[ {'Name':'Admin' ,'RoleId':'xa1234' },{'Name':'Engineer' , 'RoleId':'xa5678' }], 'Test':{'a':'A','b':'b'} }}
输出应为:

<Employee>
       <Id>TA23434</Id>
       <Name>Kesaven</Name>
       <Email>, ..... </Email>
       <Roles>
             <Roles-1>
                         <Name>Admin</Name>
                         <RoleId>xa1234</RoleId>
             </Roles-1>
             <Roles-2>
                         <Name>Admin</Name>
                         <RoleId>xa1234</RoleId>
             </Roles-2>
       <Roles>
       <Test>
             <a>A</a>
         <b>B</b>
       </Test>  
</Employee>

TA23434
凯萨文
, ..... 
管理
xa1234
管理
xa1234
A.
B

任何人都可以建议哪种方法更容易做到这一点。

您可以使用以下方法:

def to_tag(k, v):
    """Create a new tag for the given key k and value v"""
    return '<{key}>{value}<{key}/>'.format(key=k, value=get_content(k, v))

def get_content(k, v):
    """Create the content of a tag by deciding what to do depending on the content of the value"""
    if isinstance(v, str):
        # it's a string, so just return the value
        return v
    elif isinstance(v, dict):
        # it's a dict, so create a new tag for each element
        # and join them with newlines
        return '\n%s\n' % '\n'.join(to_tag(*e) for e in v.items())
    elif isinstance(v, list):
        # it's a list, so create a new key for each element
        # by using the enumerate method and create new tags
        return '\n%s\n' % '\n'.join(to_tag('{key}-{value}'.format(key=k, value=i+1), e) for i, e in enumerate(v))

d = {'Employee':{ 'Id' : 'TA23434', 'Name':'Kesavan' , 'Email':'k7@gmail.com' , 'Roles':[ {'Name':'Admin' ,'RoleId':'xa1234' },{'Name':'Engineer' , 'RoleId':'xa5678' }], 'Test':{'a':'A','b':'b'} }}

for k,v in d.items():
    print to_tag(k, v)

您可以使用以下内容:

def to_tag(k, v):
    """Create a new tag for the given key k and value v"""
    return '<{key}>{value}<{key}/>'.format(key=k, value=get_content(k, v))

def get_content(k, v):
    """Create the content of a tag by deciding what to do depending on the content of the value"""
    if isinstance(v, str):
        # it's a string, so just return the value
        return v
    elif isinstance(v, dict):
        # it's a dict, so create a new tag for each element
        # and join them with newlines
        return '\n%s\n' % '\n'.join(to_tag(*e) for e in v.items())
    elif isinstance(v, list):
        # it's a list, so create a new key for each element
        # by using the enumerate method and create new tags
        return '\n%s\n' % '\n'.join(to_tag('{key}-{value}'.format(key=k, value=i+1), e) for i, e in enumerate(v))

d = {'Employee':{ 'Id' : 'TA23434', 'Name':'Kesavan' , 'Email':'k7@gmail.com' , 'Roles':[ {'Name':'Admin' ,'RoleId':'xa1234' },{'Name':'Engineer' , 'RoleId':'xa5678' }], 'Test':{'a':'A','b':'b'} }}

for k,v in d.items():
    print to_tag(k, v)

这是典型的家庭作业。请解释一下你试过什么,然后我们也许能help@tback请不要再使用
家庭作业
标签。这是官方不推荐的。@Steak先生对不起,我不知道。不会再用了。用什么来代替“家庭作业”?我们能找到这个讨论发生在哪里的链接吗?这是一个典型的家庭作业。请解释一下你试过什么,然后我们也许能help@tback请不要再使用
家庭作业
标签。这是官方不推荐的。@Steak先生对不起,我不知道。不会再用了。用什么来代替“家庭作业”?我们能找到这个讨论发生在哪里的链接吗?谢谢Steak先生。我找到了另一种解决方法(递归法)。谢谢Steak先生。我找到了另一种解决方法(递归方法)。