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

时间:2015-09-26 20:01:44

标签: python html python-2.7 yield

说我有一个任意列表:

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>

结果:

render

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

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

基于渲染结果的blockquote标签:

render3

所以,我试图改变递归生成器(用于展平列表):

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

但实际上没有任何结果。

1 个答案:

答案 0 :(得分:0)

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

首先在我看来,词典更适合这种情况。

要保存订单,我们可以使用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。

&#13;
&#13;
<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>
&#13;
&#13;
&#13;