开发者

Creating a loop to print html lists

I need to create a html list similar in form to the following:

<ul class="treeView">
    <li>
        Root Item
        <ul class="collapsibleList">
        <li>
            Sub-Folder 1
            <ul>
                <li>
                    Sub-Sub-Folder 1
                    <ul>
                        <li>Item 1</li>
                        <li class="lastChild">Item 2</li>
                    </ul>
                </li>
                <li class="lastChild">
                    Sub-Sub-Folder 2
                    <ul>
                        <li>Item 3</li>
                        <li class="lastChild">Item 4</li>
                    </ul>
                </li>
            </ul>
        </li>
            <li class="lastChild">
                Sub-Folder 2
                <ul>
                    <li>Item 5</li>
                    <li>Item 6</li>
                    <li class="lastChild">Item 7</li>
                </ul>
            </li>
        </ul>
    </li>
</ul>

This list is based off a directory structure, but I have the code down for listing the actual structure. So all I need is how to use the for loop to keep track of everything and create the nested html lists.

Below is my python script that I have so far:

for item in lstItems:
    splitfile = os.path.split(item[0])
    webpyPath = splitfile[0].replace("/srv/http/example/www", "")
    itemName = splitfile[1]
    if item[1] == 0:
        lstRemDir.append(itemName)
    intDirNum = len(lstRemDir) - 1
    if item[1] == 0:
        if len(lstRemDir) == 1:
            print '''
<ul class="treeView">
    <li>Collapsible lists
        <ul class="collapsibleList">
 开发者_StackOverflow           '''
        elif len(lstRemDir) != 1:
            f.write('    </ul>\n</li>')
        f.write('<li><a href=\"' + webpyPath + "/" + itemName + '\" id=\"directory\" alt=\"' + itemName + '\" target=\"viewer\">' + itemName + '</a></li>\n')
    elif item[1] == 1:
        f.write('<li><a href=\"' + webpyPath + "/" + itemName + '\" id=\"file\" alt=\"' + itemName + '\" target=\"viewer\">' + itemName + '</a></li>\n')
    else:
        f.write('<li>An error happened in processing ' + itemName + '.')

The reason I am asking is because I have been trying to get this figured out for days. I am not asking for someone to do it for me, just a good way to do it. I have tried using lists to store data but I felt like it got too confusing. Hopefully that clarifies what I am looking for.

Thanks in advance!


This might help to get you started on a simpler solution that uses a nested dictionary to represent your files instead of lists:

dir = {}
start = rootdir.rfind('/')+1
for path, dirs, files in os.walk(rootdir):
    folders = path[start:].split('/')
    subdir = dict.fromkeys(files)
    parent = reduce(dict.get, folders[:-1], dir)
    parent[folders[-1]] = subdir

Here is an example of what dir might look like for a similar structure to the one in your example:

{
    "root": {
        "folder2": {
            "item2": None, 
            "item3": None, 
            "item1": None
        }, 
        "folder1": {
            "subfolder1": {
                "item2": None, 
                "item1": None
            }, 
            "subfolder2": {
                "item3": None, 
                "item4": None
            }
        }
    }
}

Using this it shouldn't be too difficult to write a recursive function that accepts a dictionary and returns an html list. Here is a simple function that just prints the items out at the correct level, you should be able to modify this to add the html code:

def html_list(nested_dict, indent=0):
    result = ''
    if nested_dict is not None:
        for item in sorted(nested_dict.keys()):
            result += ' '*indent + item + '\n'
            result += html_list(nested_dict[item], indent+4)
    return result

>>> print html_list(dir)
root
    folder1
        subfolder1
            item1
            item2
        subfolder2
            item3
            item4
    folder2
        item1
        item2
        item3
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜