AttributeError:“list”对象没有属性“split”

2024-10-05 10:49:14 发布

您现在位置:Python中文网/ 问答频道 /正文

def titleapi(value, list):
    list = str(list)
    list_ = list.split(',')
    print(list_)
    list_2 = list_.split(':')
    print(list_2)
    code = '<ul>'
    var = 0
    for ele in list_2:
        if var == 0:
            var = 1
            if ele == value:
                var_2 = 0
                for ele_2 in list_2:
                    if var_2 == 0:
                        var_2 = 1
                        if ele_2 == ele:
                            code += '\n\t<li><a class="active" href="{}">{}</a></li>'.format(list_2[(list_2.index(str(ele))) + 1], str(ele))
                        else:
                            code += '\n\t<li><a href="{}">{}</a></li>'.format(list_2[(list_2.index(str(ele))) + 1], str(ele))
        else:
            continue

    code += '\n<ul>'
    return str(code)

AttributeError:“list”对象没有属性“split”

我试图让它返回HTML代码,输入如下

titleapi(title, 'Home:#,About:#,Contact:#')

“#”符号只是因为它现在是一个死链接


Tags: inforifvaluevarcodeliul
1条回答
网友
1楼 · 发布于 2024-10-05 10:49:14

split函数用于字符串,不适用于列表。通过指定的分隔符(作为参数传递)断开给定字符串后,它返回字符串列表

您可以尝试的一件事是通过将第3行中的代码更改为list_ = str(list.split(',')),将作为列表的list_转换为字符串

相关问题 更多 >

    热门问题