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:#')
“#”符号只是因为它现在是一个死链接
split函数用于字符串,不适用于列表。通过指定的分隔符(作为参数传递)断开给定字符串后,它返回字符串列表
您可以尝试的一件事是通过将第3行中的代码更改为
list_ = str(list.split(','))
,将作为列表的list_
转换为字符串相关问题 更多 >
编程相关推荐