![](/img/trans.png)
[英]Python 2 : AttributeError: 'list' object has no attribute 'split'
[英]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.