Python:将任意结构的嵌套列表转换为html

Python: convert arbitrarily structured nested list to html

假设我有一个任意列表:

qw = ['a', [['tag', '1'], ['tag', '2']]]

我需要用 <blockquote> 构建 html(只是一个 python 字符串;列表的每个元素都应该根据层次结构包装在 blockquote 标签中):

<blockquote> a
    <blockquote> tag
        <blockquote> 1 </blockquote>
    </blockquote>
    <blockquote> tag
        <blockquote> 2 </blockquote>
    </blockquote>
</blockquote>

结果:

例如我有一个字符串 test='str1str2str34' 和一些将其拆分为列表的规则:

['str1str2str34', [
    ['str1', ['str', '1']], 
    ['str2', ['str', '2']], 
    ['str34', ['str', '3', '4']]
    ]
]

基于块引用标签的渲染结果:

因此,我尝试更改递归生成器(用于展平列表):

def flatten(l):
    for el in l:
        if isinstance(el, collections.Iterable) and not isinstance(el, basestring):
            for sub in flatten(el):
                yield sub
        else:
            yield el

但真的什么都没有。

随着时间的推移,我找到了解决方案。

首先我认为字典更适合这种情况。

要保存订单,我们可以使用 OrderedDict:

j = OrderedDict([
    ('str1str2str34', OrderedDict([
        ('str1', ['str', '1']),
        ('str2', ['str', '2']),
        ('str34', ['str', '3', '4'])
    ]))
])

我们使用递归生成器来解决这个任务:

tag = 'blockquote'

def recurse(d, tag):
    if isinstance(d, (dict, OrderedDict)):
        for k in d:
            yield '<' + tag + '>' + k
            for sub in recurse(d[k], tag):
                yield sub
            yield '</' + tag + '>'
    elif isinstance(d, (list, tuple)):
        d = ['<{1}>{0}</{1}>'.format(el, tag) for el in d]
        yield '<' + tag + '>' + ' '.join(d) + '</' + tag + '>'

print '\n'.join(list(recurse(j, tag)))

下面是美化的html.

<blockquote>str1str2str34
    <blockquote>str1
        <blockquote>
            <blockquote>str</blockquote>
            <blockquote>1</blockquote>
        </blockquote>
    </blockquote>
    <blockquote>str2
        <blockquote>
            <blockquote>str</blockquote>
            <blockquote>2</blockquote>
        </blockquote>
    </blockquote>
    <blockquote>str34
        <blockquote>
            <blockquote>str</blockquote>
            <blockquote>3</blockquote>
            <blockquote>4</blockquote>
        </blockquote>
    </blockquote>
</blockquote>