繁体   English   中英

AttributeError: 'list' object 没有属性 'split' - Python

[英]AttributeError: 'list' object has no attribute 'split' - 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' object 没有属性 'split'

我试图让它返回 HTML 代码,输入是这样的

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

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

拆分 function 用于字符串,不适用于列表。 它在通过作为参数传递的指定分隔符分隔给定字符串后返回字符串列表。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM