简体   繁体   中英

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 has no attribute 'split'

I am trying to get it to return the HTML code, the input is like this

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

The "#" sign is just because its a dead link for now

The split function is used for strings and is not available for lists. It returns a list of strings after breaking the given string by the specified separator which is passed as the parameter.

One thing you could try is converting list_ which is a list, to a string by changing the code in line 3 to list_ = str(list.split(','))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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