【问题标题】:Python: convert arbitrarily structured nested list to htmlPython:将任意结构的嵌套列表转换为html
【发布时间】:2015-09-26 20:01:44
【问题描述】:

假设我有一个任意列表:

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

但实际上没有任何结果。

【问题讨论】:

  • list(flatten(qw)).

标签: python html python-2.7 yield


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2014-08-04
    • 1970-01-01
    • 2021-02-09
    • 1970-01-01
    • 2017-03-21
    • 1970-01-01
    • 2019-10-19
    • 1970-01-01
    • 2020-04-03
    相关资源
    最近更新 更多